0

I create one JSP form and when i click on submit button my below doPost method call and this redirect to another jsp Error page with attribute "errorMessage"

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>

This scenario work perfectly but on my tomcat log i am getting below Error.

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)

I am using Java with Tomcat 8.

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
user3441151
  • 1,880
  • 6
  • 35
  • 79
  • Try removing the forward slash in page path. request.getRequestDispatcher("error.jsp"); – Ganesh S Oct 15 '16 at 10:46
  • @NarayanaGanesh like this "RequestDispatcher view = request.getRequestDispatcher("error.jsp");" – user3441151 Oct 15 '16 at 10:47
  • @NarayanaGanesh I use "request.getRequestDispatcher("error.jsp")" but same error. – user3441151 Oct 15 '16 at 10:51
  • 1
    Have you read http://stackoverflow.com/questions/2123514/java-lang-illegalstateexception-cannot-forward-sendredirect-after-response-ha ? There are a number of possible causes there, it might be useful to check whether one of the mentioned reasons applies to your case. – fvu Oct 15 '16 at 10:54
  • @fvu can you explain me? what i have to do for removing this error. – user3441151 Oct 15 '16 at 11:13

1 Answers1

0

Tomcat maintains an internal buffer where it stores (part of) the response. The most important part is the status code and the headers. If an error occurs it can discard that buffer and send an error page for instance.The same happens if you try to send a redirect.

All these is possible because that buffer can be discarded. But if it has been sendt to the client, then there is nothing to be done, the client would have already received the response code and the headers, and the redirect is performed exactly by headers (Location) and a status code (301 or 302).

So the solution to your problem is not to commit the response - e.g, calling flush() on the response output stream, not filling up that buffer thus forcing tomcat to flush it, or even better do not try to send anything to the client if you intend to make a redirect.

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82