问答详情
源自:5-3 [Java Web] 过滤器的分类3

在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)


提问者:默109 2017-08-10 11:12

个回答

  • 慕容77
    2018-01-03 11:24:32

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

  • 街角聽著屬於你的歌3606741
    2017-08-10 11:41:59

    看看web.xml的配置信息