首页 > 代码库 > Cannot create a session after the response has been committed解决方案

Cannot create a session after the response has been committed解决方案

场景:

@RequestMapping("/demo")
    public void doTmallGujia(String callbackUrl, HttpServletResponse response){
        try {
            if(StringUtils.isNotBlank(callbackUrl)){
                callbackUrl = URLDecoder.decode(callbackUrl, "UTF-8");
                response.sendRedirect(callbackUrl);
            }
        } catch (IOException e) {
            LOGGER.error("send doTmallGujia error: ",e);
        }
    }

 启动之后访问报错:

严重: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
java.lang.IllegalStateException: Cannot create a session after the response has been committed
    at org.apache.catalina.connector.Request.doGetSession(Request.java:2921)
    at org.apache.catalina.connector.Request.getSession(Request.java:2318)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:899)
    at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:911)
    at com.wupao.filter.MonitorFilter.doFilter(MonitorFilter.java:160)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.wupao.filter.CORSFilter.doFilter(CORSFilter.java:31)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

     解决方案: session放在response.sendRedirect("");之前创建即可! 代码如下:

@RequestMapping("/demo")
    public void doTmallGujia(String callbackUrl, HttpServletResponse response,HttpServletRequest request){
        try {
           request.getSession();
            if(StringUtils.isNotBlank(callbackUrl)){
                callbackUrl = URLDecoder.decode(callbackUrl, "UTF-8");
                response.sendRedirect(callbackUrl);
            }
        } catch (IOException e) {
            LOGGER.error("send doTmallGujia error: ",e);
        }
    }


本文出自 “IT技术解决方案” 博客,请务必保留此出处http://wyait.blog.51cto.com/12674066/1949700

Cannot create a session after the response has been committed解决方案