-1

I want to retrieve data from database using jsp and display it on jsp. I tried with jsp and servlet and it is working but when I want to do with only jsp its giving this error.

retrieve.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h4>Enter Employee ID and the dates</h4>
    <form name="retrieve form" action="display.jsp" method="POST">
        <table border="0">

            <tbody>
                <tr>
                    <td>Employee ID</td>
                    <td><input type="text" name="Emp_id" required="required"/></td>
                </tr>
                <tr>
                    <td>From Date:</td>
                    <td><input type="date" name="From" value="yyyy/MM/dd" required="required"/></td>
                </tr>
                <tr>
                    <td>To Date:</td>
                    <td><input type="date" name="To" value="yyyy/MM/dd" required="required"/></td>
                </tr>
            </tbody>
        </table>
        <input type="reset" value="Clear" name="clear" />
        <input type="submit" value="Submit" name="submit" />
    </form>
</body>
</html>

Display.jsp:

<%@page import="java.sql.*"%>
<%@page import="com.eis.bean.ConnectionProvider" %>
<%@page import="com.eis.bean.Provider" %>
<%@page import="java.util.Date"%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%! 
        public class RD{


        PreparedStatement ps = null; 
        ResultSet rs = null;
        public RD(){
           try{ 
        Connection conn=ConnectionProvider.getConn();  
        ps = conn.prepareStatement("SELECT * FROM timsheetdb.logintable WHERE Emp_id=? and LoginDate BETWEEN ? AND ?; ");  

           }
           catch(Exception e){
               e.printStackTrace();
           }
        }

        public ResultSet getRD(String Emp_id, String from, String to){
            try{
                ps.setString(1,Emp_id);
                ps.setString(2,from);
                ps.setString(3,to);

            }
            catch(Exception e){
               e.printStackTrace();
           }   
            return rs;
        }
        }
%>
<% String employeeid= new String();
 String date1= new String();
 String date2= new String();

 if(request.getParameter("Emp_id")!=null){
     employeeid= request.getParameter("Emp_id");
 }

 if(request.getParameter("from")!=null){
     date1= request.getParameter("from");
 }

 if(request.getParameter("to")!=null){
     date2= request.getParameter("to");
 }

 RD rd= new RD();
 ResultSet table= rd.getRD(employeeid,date1,date2);
    %>
   <table border="0">
            <tbody>
                <tr>
                    <td>Emp_id</td>
                    <td>Login Date</td>
                    <td>Login Time</td>
                    <td>Logout Time</td>
                    <td>Task</td>
                    <td>Total</td>
                    <td>Overtime</td>
                </tr>
                <% while (table.next()){ 
               %> <tr>
                    <td><%=table.getString("Emp_id") %></td>
                    <td><%=table.getString("LoginDate") %></td>
                    <td><%=table.getString("LoginTime") %></td>
                    <td><%=table.getString("LogoutTime") %></td>
                    <td><%=table.getString("task") %></td>
                    <td><%=table.getString("Total") %></td>
                    <td><%=table.getString("Overtime") %></td>
                </tr>
                <% } %>
            </tbody>
        </table>
</body>
</html>

below is the error which I got:

HTTP Status 500 - An exception occurred processing JSP page /display.jsp at line 80

type Exception report

message An exception occurred processing JSP page /display.jsp at line 80

description The server encountered an internal error that prevented it from  fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page     /display.jsp at line 80

77:                         <td>Total</td>
78:                         <td>Overtime</td>
79:                     </tr>
80:                     <% while (rs.next()){ 
81:                    %> <tr>
82:                         <td><%=rs.getString("Emp_id") %></td>
83:                         <td><%=rs.getString("LoginDate") %></td>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
org.apache.jsp.display_jsp._jspService(display_jsp.java:178)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache      Tomcat/8.0.26 logs.

please let me know how I can I resolve this.

dpk12
  • 95
  • 2
  • 16
  • 1
    A bad idea to put SQL code or any database related code in JSP. – We are Borg Oct 16 '15 at 08:11
  • Can you post your getters and setters? – bmarkham Oct 16 '15 at 08:27
  • thanks @WeareBorg I know that I have created dao and servlet pages as well... but I want to know why the error is coming in this one – dpk12 Oct 16 '15 at 08:28
  • Because you must check if the data recieved from DB is null or not and then insert it. NPE issues are always self explanatory, they mean that you are trying to work on some data which is NULL, nothing funky. – We are Borg Oct 16 '15 at 08:32

2 Answers2

0
80:                     <% while (rs.next()){ 

AND

<% while (table.next()){ 

Not the same thing there, maybe you did not deploy the latest version of your code.

but all in all that error

java.lang.NullPointerException

you need to check if rs (or table) is set before using it.

if(rs!=null)

And like We are borg said, don't do sql in jsp, just don't.

Community
  • 1
  • 1
Tahar Bakir
  • 716
  • 5
  • 14
0

Change your method like this

public ResultSet getRD(String Emp_id, String from, String to){
                try{
                    ps.setString(1,Emp_id);
                    ps.setString(2,from);
                    ps.setString(3,to);
                    rs = ps.executeQuery();
                }
                catch(Exception e){
                   e.printStackTrace();
               }   
                return rs;
            }

Earlier you were just setting the parameters in prepared statement and you were not executing the prepared statement.You was simply returning rs which you have initially kept null and never updated in your code.Thats why it failed with nullpointerexception. I hope it will give you a pointer to go in right direction.

RockAndRoll
  • 2,247
  • 2
  • 16
  • 35