user525146
user525146

Reputation: 4008

problem with request dispatcher from servlets to jsp

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);
        }

Upvotes: 0

Views: 2489

Answers (2)

Logan
Logan

Reputation: 2515

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.

Upvotes: 0

Kal
Kal

Reputation: 24910

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

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

Upvotes: 2

Related Questions