为了账号安全,请及时绑定邮箱和手机立即绑定

在Filter的doFilter中进行重定向 出现异常

我在 filter的 dofilter方法里 重定向到一个servlet 出现java.lang.IllegalStateException: Cannot create a session after the response has been committed异常是怎么回事 转发就没有问题

doFilter:

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

System.out.println("begin...AsyncFilter");

HttpServletResponse res=(HttpServletResponse) response;

HttpServletRequest req=(HttpServletRequest) request;

res.sendRedirect("/servlet/AnsycServlet");

// req.getRequestDispatcher("/servlet/AnsycServlet").forward(req, res);

chain.doFilter(request, response);

System.out.println("end...AsyncFilter"); 

}

异常信息:

严重: Servlet.service() for servlet [jsp] in context with path [/AnsycDemo] threw exception [javax.servlet.ServletException: java.lang.IllegalStateException: Cannot create a session after the response has been committed] with root cause

java.lang.IllegalStateException: Cannot create a session after the response has been committed

at org.apache.catalina.connector.Request.doGetSession(Request.java:2938)

at org.apache.catalina.connector.Request.getSession(Request.java:2379)

at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:908)

at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:920)

at org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:133)

at org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:109)

at org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:60)

at org.apache.jsp.index_jsp._jspService(index_jsp.java:100)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at filter.AsyncFilter.doFilter(AsyncFilter.java:45)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

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

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:625)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1372)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Unknown Source)


正在回答

2 回答

res.sendRedirect(req.getContextPath()+"/servlet/AnsycServlet"");重定向的路径写错了吧,要加项目跟路径

0 回复 有任何疑惑可以回复我~

看看web.xml的配置信息

0 回复 有任何疑惑可以回复我~
#1

默109 提问者

用的注解 转发都可以 就是重定向不行
2017-08-10 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

在Filter的doFilter中进行重定向 出现异常

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信