2016-10-15 57 views
0

我創建一個JSP形式,當我點擊提交按鈕,我下面doPost方法調用,這重定向到另一個JSP錯誤頁面屬性「的errorMessage」的Java Servlet以後不能轉發響應一直致力於

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(); 
    } 
} 

我錯誤jsp頁面

<%@ 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) 

我使用的Java使用Tomcat 8.

+0

嘗試在頁面路徑中刪除正斜槓。的request.getRequestDispatcher( 「error.jsp文件」); –

+0

@NarayanaGanesh like this「RequestDispatcher view = request.getRequestDispatcher(」error.jsp「);」 – user3441151

+0

@NarayanaGanesh我使用「request.getRequestDispatcher(」error.jsp「)」但同樣的錯誤。 – user3441151

回答

0

Tomcat的保持其中存儲的內部緩衝器(的一部分)的響應。最重要的部分是狀態碼和標題。如果發生錯誤,可以丟棄該緩衝區併發送錯誤頁面。如果嘗試發送重定向,也會發生同樣的情況。

所有這些都是可能的,因爲該緩衝區可以被丟棄。但是,如果它已發送給客戶端,那麼就沒有什麼可做的事情了,客戶端已經收到了響應代碼和標題,並且重定向完全由標題(位置)和狀態代碼(301或302 )。

因此,解決您的問題不是提交響應 - 例如,在響應輸出流上調用flush(),不填充該緩衝區,從而迫使tomcat刷新它,或者甚至更好的是不要嘗試發送任何內容到客戶端,如果你打算重定向。

相關問題