我正在尝试根据用户在 JSP 页面中选择的日期从数据库中检索值。用户将选择两个日期并查询数据库。
在我的 Servlet 中,我正在这样做。for 语句告诉我列表中的值是正确的。
VehiclesList list = dataManager.getVehiclesWhere(dateFrom, dateTo);
request.setAttribute("vehiclesList", list);
VehiclesList vList = (VehiclesList) request.getAttribute("vehiclesList");
for (int pos = 0; pos < vList.size(); pos++) {
Vehicles vehicles = vList.getVehicleAt(pos);
System.out.println(vehicles.getC02Emissions());
System.out.println(vehicles.getManufacture());
System.out.println(vehicles.getRegNumber());
System.out.println(vehicles.getHireRate());
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/vehicles.jsp");
dispatcher.forward(request, response);
这是我的 JSP 页面中的代码。我正在执行上述相同的过程,即请求属性、创建新列表并显示数据,但我创建的新列表显示为空,并且我收到空指针异常。
VehiclesList vList = new VehiclesList();
vList = (VehiclesList) request.getAttribute("vehiclesList");
for (int pos = 0; pos < vList.size(); pos++) {
Vehicles vehicle = vList.getVehicleAt(pos);
System.out.println(vehicle.getC02Emissions());
}
为什么 JSP 端的列表为空,而在 servlet 端它包含正确的值?
编辑 - - -
这是我的堆栈跟踪。
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.vehicles_jsp._jspService(vehicles_jsp.java from :257)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)
编辑 2------------
小服务程序代码
package Oracle;
import RentalRecords.Vehicles;
import RentalRecords.VehiclesList;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class VehiclesServlet extends HttpServlet {
private boolean dbOK = false;//boolean to check if data input is not null
private Vehicles vehicle;//used to get user data from bean
private DatabaseConnector dataManager;//used to interact with the database
/**
*
* Initialises the servlet by creating a new DatabaseConnector object which
* will be used to interface with the database
*/
public void init() {
dataManager = new DatabaseConnector();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dateFrom = request.getParameter("from");//get email submitted from form
String dateTo = request.getParameter("to");//get password submitted from form
System.out.println(dateFrom + dateTo);
if (dateFrom != null || dateTo != null) {
dbOK = true;
} else {
dbOK = false;
}//end if
HttpSession session = request.getSession(true);//set the session
if (dbOK) {
try {
System.out.println(dbOK);
VehiclesList list = dataManager.getVehiclesWhere(dateFrom, dateTo);
request.getSession().setAttribute("vehiclesList", list);
VehiclesList vList = (VehiclesList) request.getAttribute("vehiclesList");
for (int pos = 0; pos < vList.size(); pos++) {
Vehicles vehicles = vList.getVehicleAt(pos);
System.out.println(vehicles.getC02Emissions());
System.out.println(vehicles.getManufacture());
System.out.println(vehicles.getRegNumber());
System.out.println(vehicles.getHireRate());
}
RequestDispatcher dispatcher = request.getRequestDispatcher("/vehicles.jsp");
dispatcher.include(request, response);
//request.getRequestDispatcher("vehicles.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}//end try
} else {
//User is not valid, redirect back to index.jsp and inform user
session.setAttribute("signupError", "Signup Not Successful");
session.setAttribute("signupSuccess", null);//set this null if it is a resubmit
System.out.println(session.getAttribute("signupError"));
RequestDispatcher dispatcher = request.getRequestDispatcher("/vehicles.jsp");
dispatcher.forward(request, response);
}
}
}
JSP 代码
<%@page import="RentalRecords.Vehicles"%>
<%@page import="RentalRecords.VehiclesList"%>
<%@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>
<script>
$(document).ready(function() {
var from = $( "#from" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
minDate: 0,
dateFormat: 'd-M-y',
onClose: function( selectedDate ) {
$( "#to" ).datepicker( "option", "minDate", selectedDate );
}
});
var to = $( "#to" ).datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
minDate: 0,
dateFormat: 'd-M-y',
onClose: function( selectedDate ) {
$( "#from" ).datepicker( "option", "maxDate", selectedDate );
}
});
});
</script>
</head>
<body>
<form id="getdates" method="get" action="getdates">
<label for="from">From</label>
<input type="text" id="from" name="from" />
<label for="to">to</label>
<input type="text" id="to" name="to" />
<input id="submitdates" name="submitdates" type="submit" value="Search" />
</form>
<div class="header_01">Vehicles</div>
<%
VehiclesList vList = new VehiclesList();
vList = (VehiclesList) request.getSession().getAttribute("vehiclesList");
for (int pos = 0; pos < vList.size(); pos++) {
Vehicles vehicle = vList.getVehicleAt(pos);
System.out.println(vehicle.getC02Emissions());
%>
<table>
<tr>
<td class="label"><label id="ltable" for="table"></label>
<table>
<tr>
<td class="label"><label id="lbodytype" for="transmission">Body Type</label></td>
<td class="field"><input id="bodytype" name="transmission" type="text" value="<%= vehicle.getBodyType()%>" maxlength="50" readonly/></td>
<td class="status"></td>
</tr>
<tr>
<td class="label"><label id="ltransmission" for="transmission">Transmission</label></td>
<td class="field"><input id="transmission" name="transmission" type="text" value="<%= vehicle.getTransmission()%>" maxlength="50" readonly/></td>
<td class="status"></td>
</tr>
</table>
</td>
</tr>
</table>
<% }%>
</body>
</html>