How do I get a servlet container to read JSESSIONID from the query string rather than from the URL

user533020 picture user533020 · Aug 27, 2011 · Viewed 9.7k times · Source

I need to be able to maintain a session by having the JSESSIONID specified as a parameter of the query string of the url rather than part of the URL itself.

I other words I need to maintain a session like this

http://myserver.com?jsessionid=A463B23BC4F2342FA

rather than

http://myserver.com;jsessionid=A463B23BC4F2342FA

For a servlet container I'm using both tomcat 6.0 and weblogic 10.3

Reason:

I'm creating a Google Earth network link which requires me to keep a session for the requests a client makes. Google Earth doesn't support cookies and it appears there is no way to change the url that it uses to make requests. I can only tell it to append a query string parameter on subsequent requests by adding the following to the kml in my server responses

<NetworkLinkControl>
    <cookie>JSESSIONID=A463B23BC4F2342FA</cookie>
</NetworkLinkControl>

Answer

BalusC picture BalusC · Aug 27, 2011

Not possible. I'd create on your side a filter which redirects the request to the proper URL whenever an URL arrives with the JSESSIONID in the query string.

Basic kickoff example:

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    if ("GET".equals(request.getMethod()) && request.getParameter("JSESSIONID") != null) {
        response.sendRedirect(request.getRequestURL().append(";JSESSIONID=")
            .append(request.getParameter("JSESSIONID")).toString());
    } else {
        chain.doFilter(request, response);
    }
}

Map this on an URL pattern which covers requests which could potentially originate by that site. Or if there is none, just on /*.