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);
        }
    }
时间: 2024-11-13 10:12:11

Cannot create a session after the response has been committed解决方案的相关文章

Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常 java.lang.IllegalStateException: Cannot create a session after the response has been committed 之所以会出现此类问题是因为我们在Response输出响应后才创建Session的. (因为那时候服务器已经将数据发送到客户端了,即:就无法发送Session ID 了) 解决办法: 1.创建访问Session的语句[request.getSession()]提

有时候在操作Session时,系统会抛出如下异常:java.lang.IllegalStateException: Cannot create a session after the response has been committed

有时候在操作Session时,系统会抛出如下异常 java.lang.IllegalStateException: Cannot create a session after the response has been committed 原因1: Session 的创建语句: HttpSession seesion = request.getSession(); 之前有Response的输出语句. 应该把HttpSession seesion = request.getSession(); 放

Cannot create a session after the response has been committed的处理

情景:在导出excel导出过程中前台提示"正在导出,请稍等...",导出结束后提示信息自动消失. 但导出结束后就报如上错误,不可能相应两次. 解决:主要是判断导出结束的时间点,确认导出结束时间.通过cookie解决,设置cookie if(fileName.contains("日志数据")){ Cookie status = new Cookie("exportStatus","success"); status.setMax

Cannot forward after response has been committed

项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDispatcher("admin.jsp").forward(request, response); 解决方法: 原代码:request.getRequestDispatcher("admin.jsp").forward(request, response); 修改成:删除

java.lang.IllegalStateException: Cannot forward after response has been committed

jjava.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispat

解决Cannot forward after response has been committed

在是用request.getRequestDispatcher("目标地址URL").forward(request, response);时报错Cannot forward after response has been committed 字面意识是response已经提交过了不能再次提交,多次试了以后发现我的代码中有用了几次request.getRequestDispatcher("目标地址URL").forward(request, response);语句

Laravel表单篇----request请求,session存储,response响应以及中间件

请求Request 取值(传入参数Request $request) $request -> input('name');(请求name值) $request -> input('sex', '未知');(也可以请求未被定义的参数) $request -> all();(得到所有请求值) 判断取值类型 $request -> method();(判断请求类型) $request -> isMethod('GET');(判断是否为get请求) $request -> aj

Cannot forward after response has been committed 问题

1.问题的出现:当用户进入主页的时候,我需要判断是来着移动端还是pc端,于是我用了拦截器,拦截所有请求: 2.操作:1)mvc配置文件<mvc:interceptor>                                  <mvc:mapping path="/**" />                                            <bean class="com.thinkgem.jeesite.mod

myeclipse中启动时Tomcat出现Could not create the view: An unexpected exception was thrown错误解决方案

问题: 电脑突然断电,myeclipse非正常关闭,"Package Explorer"非正常显示,出现错误"Could not create the view: An unexpected exception was thrown."但是项目中的文件可以通过命令查看. 解决方式: 1.关闭myeclipse 2.删除文件".metadata/.plugins/org.eclipse.core.runtime/.settings/com.genuitec.