我想使用jsp从数据库中检索数据并将其显示在jsp上。我尝试使用jsp和servlet并且它正在工作,但是当我只想使用jsp时它会给出这个错误。
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>
下面是我得到的错误:
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.
请让我知道如何解决这个问题。
答案 0 :(得分:0)
80: <% while (rs.next()){
和
<% while (table.next()){
那里不一样,也许你没有部署最新版本的代码。
但总而言之是错误
java.lang.NullPointerException
你需要在使用之前检查是否设置了rs(或表)。
if(rs!=null)
和We are borg一样,不要在jsp中执行sql,只是不要。
答案 1 :(得分:0)
像这样改变你的方法
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;
}
之前你只是在准备好的语句中设置参数而你没有执行准备好的语句。你只是返回rs,你最初保持为null并且从未在你的代码中更新。说明为什么它失败了nullpointerexception
。
我希望它会给你一个指向正确方向的指针。