场景:
1 2 3 4 5 6 7 8 9 10 11 | @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); } } |
启动之后访问报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 严重: 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(“”);之前创建即可! 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 | @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); } } |
本文转自 wyait 51CTO博客,原文链接:http://blog.51cto.com/wyait/1949700,如需转载请自行联系原作者