`

java.lang.IllegalStateException: Cannot forward after response has been committe

 
阅读更多

当发生在如下错误的时候,有一个方案可行,

 

"java.lang.IllegalStateException: Cannot forward after response has been committed “

 

当你有多个跳转的页面的语句时候,好好检查一下,当 转发 或者 重定向的时候,需要在后加一个retrun 即可。

服务器中不允许多次跳转,否则会下面这个错误。

 

因为服务器端使用sendRedirect跳转到客户端的时候,不能在使用req.getRequestDispatcher("跳转的页面").forward(req, reqs);

跳转;

 

所以在跳转之后,return就不会往下执行。

 

 

	protected void login(HttpServletRequest req, HttpServletResponse reqs) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if(req.getUserPrincipal()!=null){
			String userID = req.getRemoteUser();
			boolean bol = req.isUserInRole(USER_ROLE_ADMIN);
			UserModel userModel =connectionDao.getUserModelByUserID(userID);
			if(bol){
				req.getSession().setAttribute("user",userModel);
				reqs.sendRedirect(req.getContextPath()+"/admin/main.jsp");
				return;
			}else{
				reqs.sendRedirect(req.getContextPath()+"/index.jsp");
				return;
			}
		}
	}

 

 

错误信息:

严重: Servlet.service() for servlet [jsp] in context with path [/TomcatSecurityValidte] threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
 at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:339)
 at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
 at com.pack.security.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:39)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:582)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
 at java.lang.Thread.run(Thread.java:619)
                                                   

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics