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.

Accepted Answer

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.session.id%>"); 
...
</script>

UPDATE:

Actually, the scriplet should be <%=pageContext.getSession().getId()%>.

The line:

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

becomes:

xhr.open("GET", url + ";jsessionid=6EBA4F94838796DC6D653DCA1DD06373");
Written by Maurice Perry
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki