JQuery jsession cookie is not sent to a server

Dmitry Buzolin picture Dmitry Buzolin · Mar 23, 2012 · Viewed 9.3k times · Source

I've searched jquery forum, stackoverflow, google, bing, and even yahoo w/o success. Every 10 sec I'm trying to load a text data from logservlet servlet via this JQuery snippet:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
            var autorefresh = setInterval(function () {
                $.ajax({
                    url : "logservlet?devkey=chat",
                    success : function(data) {
                        $("#log_ta").append(data);
                    }
                });
                }, 10000);
    </script>

The problem is, on a server side I don't see a valid session where I try to track the session attributes. The problem seems to be related to missing "Cookie JSESSIONID=xxxxxxxxxxxxxxxxxxxxxxxxxxx" in the header of the http requests from JQuery. I'm getting response headers from the server, with JSESSIONID always changing with each request:

    Server  Apache-Coyote/1.1
    Set-Cookie  JSESSIONID=9EEAFA2A933E7742D8FEDADD5345B76D; Path=/CumulusServer
    Content-Length  0
    Date    Fri, 23 Mar 2012 13:02:08 GMT

But JQuery doesn't use it subsequently, here are the request headers:

    Host    192.168.1.11:8080
    User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
    Accept  */*
    Accept-Language en-us,en;q=0.5
    Accept-Encoding gzip, deflate
    Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Connection  keep-alive
    X-Requested-With    XMLHttpRequest
    Referer http://192.168.1.11:8080//CumulusServer/

What's the issues here? Are "$.ajax" calls not session aware? Or do I miss some plumbing code to hardcode the JSESSIONID manually in each ajax request? Is so, how this should look like? Btw, when I call the same url from web browser the JSESSIONID header is sent to the server!

Thanks, D.

Answer

Dmitry Buzolin picture Dmitry Buzolin · Mar 27, 2012

Indeed I do run Adblock Plus, (thanks for the tip) but it has nothing to do with this. Actually the thing is a bit tricky. I found two solutions, but I don't have explanation what is going on.

Solution #1: - saving JSESSIONID manually when page loads:

    var Session = {
       id : '${pageContext.session.id}',
       user : '${pageContext.request.remoteUser}'
    };

    $(window).load(function () {
        setCookie('JSESSIONID',Session.id);
    });

    function setCookie(name,value,expires,path,domain,secure) {
        var cookieString = name + "=" +escape(value) +
           ( (expires) ? ";expires=" + expires.toGMTString() : "") +
           ( (path) ? ";path=" + path : "") +
           ( (domain) ? ";domain=" + domain : "") +
           ( (secure) ? ";secure" : "");
        document.cookie = cookieString;
    }

This works fine and JSESSIONID gets send over in next ajax requests accordingly.

Solution #2: - Adding "text/plain" mime type in servlet doGet() method. Initially in my test I have not set any content type at all. After I realized that JQuery is expecting XML response by default, I changed my server doGet() method to set the content type explicitly like so:

resp.setContentType("text/plain");

Now, ajax call works fine too, and no manual cookie management is needed actually. I don't have explanation why Solution #2 works, but it is.

Thanks, D.