我在写这几行代码,将属性从JSP
传递到servlet
。我很困惑为什么它不起作用,尽管我看不出任何不起作用的原因。这里的链接看起来像]
www.example.com/costvote?token=22323232323232
my.jsp
<input type="hidden" name="token" value="<%= request.getParameter("token") %>" />
<%
String token = request.getParameter("token");
request.setAttribute("token", token);
%>
</div>
<%
List<candidate> candlist = (List<candidate>) request.getAttribute("candidatelist");
for(candidate div: candlist) {
%>
<div class="row">
<div class="col-md-2"><h4><%= div.getFirstname() %></h4></div>
<div class="col-md-2"><h4><%= div.getSurname() %></h4></div>
<div class="col-md-2"><h4><%= div.getFaculty() %></h4></div>
<div class="col-md-4">
<div class="btn-group" role="group">
<a href="/assignvote?cand_id=<%= div.getId() %>" class="btn btn-primary">Vote</a>
</div>
</div>
</div>
<% } %>
</div>
Servlet.java
String idOfCandidate = (String) request.getParameter("cand_id");
String Votertoken = (String) request.getAttribute("token");
log.info(Votertoken + " "+ idOfCandidate );
我得到的输出是:
Null 323424234234
一切都正确,但似乎仍然无法正常工作。
您应该使用request.getParameter()提取请求参数
String idOfCandidate = (String) request.getParameter("cand_id");
String Votertoken = (String) request.getParameter("token");
log.info(Votertoken + " "+ idOfCandidate );
仅用于服务器端的getAttribute:如果您要将属性从servlet传递到jsp,则>]