0

I recently started studying JSP and Servlets. I am facing problems forwarding the dataList (rows from database) to JSP using requestdispatcher. In fact I have error in my JSP page which I couldn't figure out. Since I am learning I couldnt find where the error is:

Thank you!

The following is the error I get

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)

my code in JSP is:

<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>

My code in servlets is

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);
        }
Kal
  • 24,724
  • 7
  • 65
  • 65
user525146
  • 3,918
  • 14
  • 60
  • 103
  • 1
    It's fine that you're learning JSP/Servlets. The separation of controller and view in your example is very good. However, the model is very poor (it should have been a collection of Javabeans) and you're using *scriptlets* in JSP instead of taglibs/EL. I'd suggest to learn that as the next step. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files and http://stackoverflow.com/questions/1727603/places-where-javabeans-are-used. – BalusC Aug 18 '11 at 14:37

2 Answers2

2

You are not selecting contactno, dob and ssn in your query.

Modify your query to say select name, contactno, dob, ssn from employeeinfo WHERE ....

Kal
  • 24,724
  • 7
  • 65
  • 65
0

As @Kal correctly said that you forgot to add the data to the list, hence when you try to use itr.next(), it finds nothing and hence the Exception.

The Better approach will be to check the value of itr.hasNext() before using itr.next(). You may also prefer using a temporary variable to store the value returned by itr.next() if you want to use the same value over and over again.

Logan
  • 2,445
  • 4
  • 36
  • 56