我正在建立一个简单的火车预订系统,现在我试图调用一个servlet,一旦我打开一个jsp从数据库中选择数据然后返回到相同的servlet,但我一直在收到错误。
这是我的trip.jsp
<body>
<% if ((request.getAttribute("trips") == null)) {
%>
<jsp:include page="/trips" flush="true" />
<% } %>
<table>
<tr>
<th>Trip Number</th>
<th>Train Number</th>
<th>Source</th>
<th>Destination</th>
<th>Departure Time</th>
<th>Arrival Time</th>
<th>Date</th>
<th>Reserved Seats</th>
<th>Price</th>
</tr>
<%
//ArrayList list = new ArrayList();
List trip = new ArrayList();
Iterator viewTrip;
if(request.getAttribute("trips")!=null && request.getAttribute("trips")!=""){
List allTrips = (ArrayList)request.getAttribute("trips");
Iterator itr = allTrips.iterator();
while(itr.hasNext()){
trip = (ArrayList)itr.next();
viewTrip = trip.iterator();
%>
<tr>
<%
while(viewTrip.hasNext()){
%>
<td>
<%=viewTrip.next()%>
</td>
<%
}
%>
</tr>
<%
}
}
%>
</table>
</body>
这是我的servlet。 trips.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
Connection con = DB.getActiveConnection();
Statement stmt = con.createStatement();
ResultSet rs = null;
ArrayList trip = new ArrayList();
ArrayList data = null;
try {
String query = "select * from trips";
rs = stmt.executeQuery(query);
while(rs.next()){
data = new ArrayList();
data.add(rs.getString(9)); // trip number
data.add(rs.getInt(1)); // train number
data.add(rs.getString(2)); // source
data.add(rs.getString(3)); // destination
data.add(rs.getTime(4)); //departure time
data.add(rs.getTime(5));// arrival time
data.add(rs.getDate(6));// date
data.add(rs.getInt(7));// reserved seats
data.add(rs.getDouble(8));//price
trip.add(data);
}
request.setAttribute("trips", trip);
con.close();
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/updateTrip.jsp");
requestDispatcher.include(request, response);
//out.close();
//return;
}
catch (Exception e) {
e.printStackTrace();
}
}
这是我得到的错误
25-Dec-2016 15:47:01.822 SEVERE [http-nio-8084-exec-4]
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/Train] threw exception [java.lang.IllegalStateException: Exception occurred when flushing data] with root cause
java.io.IOException: Stream closed
at org.apache.jasper.runtime.JspWriterImpl.ensureOpen(JspWriterImpl.java:200)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:105)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:184)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
at org.apache.jsp.updateTrip_jsp._jspService(updateTrip_jsp.java:199)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:2)
您正在使用try with resources
,它会在退出try块时自动关闭流。
try (PrintWriter out = response.getWriter()) {
....
}
trip.jsp
将请求和响应对象传递给servlet trips.java
,在PrintWriter
中,您获得响应的try with resource
(由于某种原因,不在任何地方使用它){{1}这将关闭try块末尾的底层流。现在,当您的包含完成并且控件返回到trip.jsp
并尝试编写jsp内容时,它会失败,因为该流已经关闭。
只需将您的代码更改为:
PrintWriter out = response.getWriter();
try {
....
} catch (IOException e) {
....
}
或者如果您不想在此处理此例外,请完全删除该尝试。