我想處理表中的特定行(用戶)。有很多用戶。我想從表中獲取user_id,單擊並顯示在另一頁上。現在,我正在使用信息表單。問題是Profile.jsp頁面始終顯示相同的user_id(第一個例如2)?從jsp中獲取表的行
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它= usersContainer.iterator();檢查調試中的it.next()是什麼。 –