我想根據用戶在JSP頁面中選擇的日期選擇從數據庫檢索值。用戶將選擇兩個日期並查詢數據庫。從servlet傳遞列表到jsp null
在我的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端的列表爲null,當它在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)
EDIT 2 ------------
servlet代碼
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>
請發佈堆棧跟蹤。 –
Ok編輯發佈我的堆棧跟蹤 – user667430
@ user667430請嘗試使用'request.getSession()。setAttribute()'和'request.getSession()。getAttribute()'來測試它是否工作。只是爲了測試。我並不是說這是無狀態webapps的正確方法。 – Subin