异常信息:java.lang.IllegalStateException: Cannot forward after response has been committed

1.给后台加了登录过滤器

 

 

2. 登录后出现异常:

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 com.zuikc.bookstore.admin.web.filter.AdminFilter.doFilter(AdminFilter.java:32)

         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:94)

         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)

         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141

这个异常意味response已经被提交,不能再跳转了。这个错误的出现是因为response多次提交或在跳转到页面后仍有跳转请求。

3.好像过滤器有问题:

异常信息中有这么一句: at com.zuikc.bookstore.admin.web.filter.AdminFilter.doFilter(AdminFilter.java:32)

4.过滤器代码如下:

         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

                   /*

                    * 从session中获取admin

                    * 如果admin!=null,放行

                    * 如果为null,保存异常信息到request域,转发到login.jsp

                    */

                            HttpServletRequest httpRequest = (HttpServletRequest) request;

                            Admin admin = (Admin) httpRequest.getSession().getAttribute("session_admin");

                            if(admin != null)chain.doFilter(request, response);                          

                            request.setAttribute("msg", "未登录用户不能访问访问此页面,请先登录");

                            httpRequest.getRequestDispatcher("/adminjsps/login.jsp").forward(httpRequest, response);                                               

         }

5.解决方法:

if判断这里出了问题,上面的代码导致放行后后面的代码还是执行了,即又放行又转发。所有才会提示:Cannot forward after response has been committed

修改代码如下:

或者:

 

posted on 2019-03-29 11:38  rachelgarden  阅读(1490)  评论(0编辑  收藏  举报

导航