Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get session by id in ajax call

Is there a way to have access to session in a AJAX call made to a JAVA server.

On the server, the request object has both the session and cookies properties NULL.

I can pass though the session id as a parameter, but how can I access the session by ID?

Edit

Using session.getSession(false); returns null, while session.getSession(true); obviously returns a new session, with another id.

like image 814
Gabriel Diaconescu Avatar asked Nov 29 '25 01:11

Gabriel Diaconescu


1 Answers

The best way to deal with this is to append ";jsessionid=" at the end of the url. For instance, in a jsp:

<script type="text/javascript">
...
    xhr.open("GET", url + ";jsessionid=<%=pageContext.getSession().getId()%>"); 
...
</script>

The line:

xhr.open("GET", url + ";jsessionid=<%=pageContext.getSession().getId()%>");

is rendered as:

xhr.open("GET", url + ";jsessionid=6EBA4F94838796DC6D653DCA1DD06373");
like image 116
Maurice Perry Avatar answered Nov 30 '25 13:11

Maurice Perry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!