0

I have javascript that calls a function to set the session value like

below is the client code:

   <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); 
               }
            }
          }

Inside this Toggle.jsp i have a methood:

<%
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
    }
%>

now i am trying to fetch this session values after the page reload from the same application in different method as below :

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
   }

from another java method Test. i get it as null. can anyone please suggest me how to get the session value that is set for abc even after reloading the page?

Sangeetha cg
  • 81
  • 5
  • 14
  • Show your client-side code. If you reload the page, the in-flight request from the click handler may be cancelled by the browser. Don't reload before that request (and it's session work) is completed. Also, what is "another java program"? Not part of the same web application (then it won't get access to the session)? – Thilo Apr 15 '16 at 07:59
  • i have added the client side code as well , please take a look – Sangeetha cg Apr 15 '16 at 08:14
  • check your `trainingAvailableFlag` check whether it is `true` or `1`. Just add else case in ajax response and check the response. – Santhucool Apr 15 '16 at 09:23
  • it is true, is there anything wrong i am doing in the above code? – Sangeetha cg Apr 15 '16 at 12:45

1 Answers1

0

First let's answer the title : No it doesn't invalidate the server's session your error is not here.

Second : what is the point of this ?

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

There is no point of doing this

Finally, we'll need more information in order to resolve definitively your problem. Like the condition that provoke your javascript to be executed, or the whole HTML around the javascript if it's included in the page.

Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • the javascript function is exceuted on the click of link. we just have one link on the click of the link i have this confirmTechSimulationSwitch function invoked and for if(res !=null && res =='true'){ var res="true"; [..] } i have edited the question please – Sangeetha cg Apr 15 '16 at 08:28
  • But what's the point with var res = "true" and then window.location.reload() ? Since you reload your page all javascript context will be dropped. If you want to load the current session of the user you should do it when the page is loading. – Walfrat Apr 15 '16 at 08:50
  • i have added for some further logic purpose that has no significant for that current variable with window.location.reload – Sangeetha cg Apr 15 '16 at 09:03
  • Try to change HttpSession session = request.getSession(); by HttpSession session = request.getSession(false); If the session doesn't exists the rest of your code should trigger a NullPointerException, this means that the session token wasn't supplied by your front client. If not you're definitively sure that you're token is here. – Walfrat Apr 15 '16 at 12:45
  • please have a look , i have updated the question now, may you might get some idea now – Sangeetha cg Apr 15 '16 at 13:03
  • open your browser debogguer and check if it's is sending the proper Cookie (the JSESSIONID one) when you request the servlet. – Walfrat Apr 15 '16 at 13:05
  • please review the latest updated question i have changed it to jsp now – Sangeetha cg Apr 15 '16 at 13:16
  • I saw it but my question is still the same. Check for JSESSIONID Cookie in request and response header in your browser debogguer. – Walfrat Apr 15 '16 at 13:17
  • it's in the "network" tab generally. What browser do you have ? Can you use Firefox/Chrome ? – Walfrat Apr 15 '16 at 13:27
  • yeah i can see that in request header JSESSIONID=+HaxUlbnynPEw6Y0WbsImxtT; this is different from my application that i hit when it reloads on window.location.reload(true); http://mylocalportal.test.com:8080/sample;jsessionid=s75g9PK8eTmae3atXhHCFmcw – Sangeetha cg Apr 15 '16 at 13:30
  • Try by changing the line with **var url** with the following : var url = "/dummy-proj/Toggle.jsp". And both of your code sample as part of the sample application or do they have different URL ? if so can you show us the two URL ? – Walfrat Apr 15 '16 at 14:10
  • i changed the URL as mentioned by you , i was able to reload the application again .still i am not able to fetch the session value. for second question i have the same URL for both the code sample. – Sangeetha cg Apr 15 '16 at 14:20
  • So they're both under mylocalportal.test.com:8080/ ? No domain name different no port different ? And why do you have JESSIONID in the URL ? It's not supposed to be there. – Walfrat Apr 15 '16 at 14:26
  • yeah we have liferay application that generats the jsessionID for every hit of the application. but both of the code are in same application – Sangeetha cg Apr 15 '16 at 14:28
  • Then you should add the jsessionid in the url that you use in your ajax request i guess – Walfrat Apr 15 '16 at 14:30
  • jsessionid has nothing to do with this i guess , not sure will try to do , is there any other suggestion that we can get the session object – Sangeetha cg Apr 15 '16 at 14:33
  • You should edit your question and add the fact you're using liferay (and add the tag liferay too). I know that this product can really be annoying so liferay people should be able to help you way more than me. – Walfrat Apr 15 '16 at 14:34