zoukankan      html  css  js  c++  java
  • 异常信息: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

    修改代码如下:

    或者:

  • 相关阅读:
    软件测试中桩模块与驱动模块的概念与区别(转载),打桩
    DataFactory使用和注意,排列组合
    SCWS中文分词,功能函数实例应用
    按指定长度截取中英文混合字符串
    CSS截取中英文混合字符串长度
    使DIV相对窗口大小左右拖动始终水平居中
    浮动5-常用列表显示(案例)
    多选项卡切换原理
    使当前对象相对于上层DIV 水平、垂直居中定位
    使图片相对于上层DIV始终水平、垂直都居中
  • 原文地址:https://www.cnblogs.com/rachelgarden/p/10620268.html
Copyright © 2011-2022 走看看