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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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解决方案的主要内容,如果未能解决你的问题,请参考以下文章

cannot create windows service for mysql

configure error C compiler cannot create executables错误解决

Virtualbox 共享文件夹 cannot create symlink error

weblogic 整合cxf 报错:cannot create a secure XmlInputFactory

Cannot create a secure XMLInputFactory,

Cannot create a new pixel buffer adaptor with an asset writer input that has already started writing