我最近开始学习 JSP 和 Servlet。我在使用 requestdispatcher 将 dataList(数据库中的行)转发到 JSP 时遇到问题。事实上,我的 JSP 页面中有错误,我无法弄清楚。由于我正在学习,我找不到错误在哪里:
谢谢!
以下是我得到的错误
org.apache.jasper.JasperException: An exception occurred processing JSP page /dataPage.jsp at line 29
26: %>
27: <tr>
28: <td width="100"><%=itr.next()%></td>
29: <td width="100"><%=itr.next()%></td>
30: <td width="100"><%=itr.next()%></td>
31: <td width="100"><%=itr.next()%></td>
32: </tr>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:519)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
AuthenticationServlet.doPost(AuthenticationServlet.java:60)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我在 JSP 中的代码是:
<table border="1" width="400">
<tr>
<td width="100"><b>Name</b>
</td>
<td width="100"><b>Contact No</b>
</td>
<td width="100"><b>SSN</b>
</td>
<td width="100"><b>Date of Birth</b>
</td>
</tr>
<%
List<String> data = (List<String>)request.getAttribute("data");
Iterator<String> itr = data.iterator();
while (itr.hasNext()) {
%>
<tr>
<td width="100"><%=itr.next()%></td>
<td width="100"><%=itr.next()%></td>
<td width="100"><%=itr.next()%></td>
<td width="100"><%=itr.next()%></td>
</tr>
<%
}
%>
</table>
我在 servlet 中的代码是
List<String> dataList = new ArrayList<String>();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url);
conn.createStatement();
stmt = conn.prepareStatement("SELECT name,password FROM employeeinfo WHERE name=? AND password=?");
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
if(rs != null) {
while(rs.next()) {
int i = rs.getRow();
if(i == 1) {
dataList.add(rs.getString("name"));
dataList.add(rs.getString("contactno"));
dataList.add(rs.getString("ssn"));
dataList.add(rs.getString("dob"));
}
}
} else {
out.println("Invalid User !!");
}
} catch(Exception e) {
e.printStackTrace();
}
request.setAttribute("data", dataList);
// Dispatch the request
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if(dispatcher != null) {
dispatcher.forward(request, response);
}