window.location.reload(true) will clear HttpSession?

window.location.reload(true) will clear HttpSession?

本文关键字:will clear HttpSession true location reload window      更新时间:2024-07-02

我有一个javascript,它调用一个函数来设置会话值,比如

以下是客户端代码:

   <a id="sampleId "href="javascript:confirmTechSimulationSwitch('testLink');
 function confirmTechSimulationSwitch(){
 var url = window.location.protocol + "//" + window.location.host +   "/dummy-proj/Toggle.jsp";
 var xmlHttpReq = new XMLHttpRequest();  
    if (window.XMLHttpRequest) {
      xmlHttpReq = new XMLHttpRequest();
      xmlHttpReq.open("POST",url, false);
      xmlHttpReq.send(null);
    } 
     if (xmlHttpReq.readyState == 4) { 
            if (xmlHttpReq.status == 200){
              var newResponse =xmlHttpReq.responseText.slice(0,4);
               if(xmlHttpReq.responseText !=null && newResponse =='true'){
          window.location.reload(true); 
               }
            }
          }

在这个Toggle.jsp中,我有一个方法:

<%
System.out.println("inside jsp page");
        session.setAttribute("abc","true");
        request.setAttribute("abc","true");
        request.getSession().setAttribute("abc","true");
System.out.println(" abc123........ "+session.getAttribute("abc"));//prints true
System.out.println("abc456....... "+request.getAttribute("abc"));//prints true
System.out.println("abc789......."+request.getSession().getAttribute("abc"));//prints true
    }
%>

现在,我正试图在页面重新加载后,用不同的方法从同一个应用程序中获取这个会话值,如下所示:

public void Test(HttpServletRequest request){
HttpSession session = request.getSession();
session.getAttribute("abc");// prints null
request.getAttribute("abc");//prints null
request.getSession().getAttribute("abc")//prints null
   }

来自另一个java方法Test。我把它记为空。有人能建议我如何在重新加载页面后获得为abc设置的会话值吗?

首先让我们回答这个标题:不,它不会使服务器的会话无效——错误不在这里。

第二:这有什么意义?

if(res !=null && res =='true'){
     var res="true";
     [..]
}

做这个没有意义

最后,我们需要更多的信息,以便最终解决您的问题。比如触发javascript执行的条件,或者javascript周围的整个HTML(如果它包含在页面中)。