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

情景:在导出excel导出过程中前台提示“正在导出,请稍等...”,导出结束后提示信息自动消失。

但导出结束后就报如上错误,不可能相应两次。

解决:主要是判断导出结束的时间点,确认导出结束时间。通过cookie解决,设置cookie

 if(fileName.contains("日志数据")){
        Cookie status = new Cookie("exportStatus","success");
        status.setMaxAge(60000); //单位为秒
        response.addCookie(status);
   }

如果不设置过期时间,则表示这个cookie生命周期为浏览器会话期间,只要关闭浏览器窗口,cookie就消失了。

这种生命期为浏览会话期的cookie被称为会话cookie。会话cookie一般不保存在硬盘上而是保存在内存里。

如果设置了过期时间,浏览器就会把cookie保存到硬盘上,关闭后再次打开浏览器,这些cookie依然有效直到超过设定的过期时间。存储在硬盘上的cookie可以在不同的浏览器进程间共享,比如两个IE窗口。而对于保存在内存的cookie,不同的浏览器有不同的处理方式。

cookie.setmaxage设置为0时,会马上在浏览器上删除指定的cookie

cookie.setmaxage设置为-1时,代表关闭当前浏览器即失效。

//获取cookiefunction getCookie(name){
         var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
         if(arr=document.cookie.match(reg)){
               return unescape(arr[2]);
         }else{
               return null;
         }
 }
//删除cookie
function delCookie(name){
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval=getCookie(name);
        if(cval!=null){
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        }
}
            

在备份中添加定时器,每100ms执行一次

var timeRequest;
      $("#btnExport").click(function(){
         top.$.jBox.confirm("确认要导出日志数据吗?","系统提示",function(v,h,f){
             if(v=="ok"){
                  var currentTime = $("#currentTime").val();
                  var beginDate = $("#beginDate").val();
                  var endDate = $("#endDate").val();
                  $("#searchForm").attr("onsubmit","loading(‘正在导出数据,请稍等...‘);");
                  $("#searchForm").attr("action","${ctx}/sys/log/export?currentTime="+currentTime+"&beginDate="+beginDate+"&endDate="+endDate);
                  $("#searchForm").submit();
                  timeRequest = setInterval(refeshPage,100);
                }
           });
              top.$(‘.jbox-body .jbox-icon‘).css(‘top‘,‘55px‘);
       });
refeshPage方法如下:
function refeshPage(){
       var exportStatus = getCookie(‘exportStatus‘);
       if(exportStatus == "success"){   //获取cookie后去掉页面提示,重新提交后台获得日志列表
            clearInterval(timeRequest);  //清除定时器
            delCookie("exportStatus");   //删除cookie
            $("#searchForm").attr("action","${ctx}/sys/log/list");
            $("#searchForm").removeAttr("onsubmit");
            $("#searchForm").submit();
       }
 }

没想到用cookie,办法同事想到的,记录一下

原文地址:https://www.cnblogs.com/person008/p/9106243.html

时间: 2024-10-11 14:18:28

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解决方案

场景: @RequestMapping("/demo")     public void doTmallGujia(String callbackUrl, HttpServletResponse response){         try {             if(StringUtils.isNotBlank(callbackUrl)){                 callbackUrl = URLDecoder.decode(callbackUrl, "UT

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

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed解读

源代码: @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String uri = request.getRequestURI(); if(pathMatcher.match("/", uri)) { System.err.println("跳转"); resp