关于Filter中的两个异常,重定向后要加Return

编程技术  /  houtizong 发布于 3年前   172

这是个很常见的问题,度娘和谷歌动手就能查到,可是今天还是遇到了,因为一直做业务研发,没有直接关注这些细节。

该文章来自ITeye,网络爬虫请自重!欢迎大家访问我的博客

这两个异常如下:

java.lang.IllegalStateException: Cannot forward after response has been committedat org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:313)at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)at com.servlet.MyServlet2.doGet(MyServlet2.java:22)at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at com.filter.MyFilter.doFilter(MyFilter.java:48)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)at java.lang.Thread.run(Unknown Source)

 

java.lang.IllegalStateExceptionat org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:662)at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:325)at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at com.filter.MyFilter.doFilter(MyFilter.java:48)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)at java.lang.Thread.run(Unknown Source)

 

只要在重定向后加return就可以了。

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");HttpServletRequest hrequest = (HttpServletRequest)request;String path = hrequest.getContextPath();String basePath = hrequest.getScheme()+"://"+hrequest.getServerName()+":"+hrequest.getServerPort()+path+"/";// 获得请求的全路径String reUrl = hrequest.getRequestURL().toString();// 是否包含参数int index = reUrl.indexOf("?");String re = "";if(index == -1){re = reUrl.substring(basePath.length());}else{re = reUrl.substring(basePath.length(), index);}// 是否是请求资源if(!isResource(re)){// 是否有访问的权限boolean isCheck = ConnUril.checkUrl(re);if(!isCheck){request.getRequestDispatcher("/error.jsp").forward(request,response);// 必须加返回,否则报错return ;}}// 继续执行chain.doFilter(request, response);}

 

报错原因很简单,转向后再去执行关于response的操作,就等于执行了两遍转向,因此会报错!

 

问题简单,也需要留意,再简单的问题也是问题,成功在细节和那些别人以为是傻冒的问题中。

 

请您到ITEYE看我的原创:http://cuisuqiang.iteye.com

或支持我的个人博客,地址:http://www.javacui.com

 

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客