【发布时间】:2016-01-02 12:28:59
【问题描述】:
我想处理表中的特定行(用户)。有很多用户。我想从表中获取 user_id,单击并显示在另一个页面上。现在,我正在使用邮寄表格。问题是 Profile.jsp 页面始终显示相同的 user_id(第一个例如 2)?
jsp文件:
//...
<table>
<tr>
<th>user_id</th>
<th>user_email</th>
<th>user_password</th>
<th>Show</th>
</tr>
<%
List<String> usersContainer = UsersWorker.GetUsers();
Iterator<String> it = usersContainer.iterator();
while (it.hasNext()) {
out.print("<tr>");
for (int i = 0; i < 2; ++i) {
if(i==0) {
%><form action="ProfileServlet" method="post"><%
int user_id = Integer.parseInt(it.next());
%><td><input style="width: 30px" type="text" name="user_id" value="<% out.print(user_id); %>" readonly></td><%
}
out.print("<td>");
out.print(it.next());
out.print("</td>");
if(i==1) {
%><td><input type="submit" value="Show"></td><%
}
%></form><%
}
out.print("</tr>");
}
%>
</table>
Servlet java 文件:
//...
@WebServlet("/ProfileServlet")
public class ProfileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String user_id = request.getParameter("user_id");
System.out.print("user = " + user_id);
request.setAttribute("user_id", user_id);
getServletContext().getRequestDispatcher("/Index.jsp?subpage=6").forward(request, response);
}
}
配置文件jsp文件:
<title>Profile</title>
<b>Profile</b>
<%
String action = (String) request.getAttribute("user_id");
%><br>user_id : <%=action %>
【问题讨论】:
-
我猜问题出在 Iterator
it = usersContainer.iterator();在调试中检查 it.next() 是什么。
标签: java database forms jsp servlets