首页 > 代码库 > 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解决方案
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。