1. 程式人生 > >Cannot create a session after the response has been committed的處理

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

span 表示 ESS mov ssi AC clear 判斷 cti

情景:在導出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時,代表關閉當前瀏覽器即失效。

//獲取cookie
function
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,辦法同事想到的,記錄一下

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