Java Servlet在提交响应后无法转发

时间:2016-10-15 10:43:40

标签: java jsp tomcat servlets

我创建了一个JSP表单,当我点击提交按钮时,我的下面是doPost方法调用,并且重定向到另一个带有属性" errorMessage"

的jsp错误页面
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
            request.setAttribute("errorMessage", "You are not authorized to access the Hub System.");
            RequestDispatcher view = request.getRequestDispatcher("/error.jsp");
            view.forward(request, response);
        }
    } catch (ServletException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

My Error jsp page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <table align="center">
            <tr align="center">
                <td><img src="images/logo.jpg" /></td>
            </tr>
            <tr>
                <td>
                    <p style="color: red;">${errorMessage}</p>
                </td>
            </tr>
        </table>
    </body>
    </html>

这种情况完美无缺,但在我的tomcat日志中,我遇到了错误。

java.lang.IllegalStateException: Cannot forward after response has been committed
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
    at otn.aitc.io.MainServlet.doPost(MainServlet.java:203)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    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:141)
    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:522)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
    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)

我在Tomcat 8中使用Java。

1 个答案:

答案 0 :(得分:0)

Tomcat维护一个内部缓冲区,用于存储(部分)响应。最重要的部分是状态代码和标题。如果发生错误,它可以丢弃该缓冲区并发送错误页面。如果您尝试发送重定向,也会发生同样的情况。

所有这些都是可能的,因为可以丢弃该缓冲区。但是如果已经发送到客户端,则没有什么可做的,客户端已经收到了响应代码和标题,并且重定向完全由标题(位置)和状态代码(301或302)执行)。

所以问题的解决方案不是提交响应 - 例如,在响应输出流上调用flush(),而不是填充缓冲区从而强制tomcat刷新它,甚至更好的不尝试发送如果您打算进行重定向,请向客户提供任何内容。