Problems with getThreadLocalRequest().getSession()

918 views
Skip to first unread message

KlausG

unread,
Mar 3, 2007, 11:52:25 PM3/3/07
to Google Web Toolkit
Hi

The information stored in the HttpSession doesn't survive between
requests, after some debugging I found that I get different
HttpSession objects returned.

I have the following code (in my RemoteServiceServlet):

HttpSession httpSession = this.getThreadLocalRequest().getSession();
System.out.println("Http Session id" + httpSession.getId());

The result is:
Http Session id54AE86F55CABC4A3BF0927515DFA69FA
Http Session id918E5E77E2EFBF43A37A35CE89E09F54

I'm using gwt-windows-1.3.3 and the problem occurs both in hosted
mode, and when deployed on Tomcat in Web mode.

/Klaus

KlausG

unread,
Mar 4, 2007, 12:11:06 AM3/4/07
to Google Web Toolkit
After some futher debugging i found the problem.

Internet explorer uses 2 threads client-side, so my request hits the
server at aproximatly the same time.
Since the JSessionID cookie, which controlles the HTTPSession isn't
set before the client recieves the response, the server cannot tell
that the two requests comes from the same client.
The reason this suprised me was that I had made a bunch of other RPC
calls to my servlet, but since I didn't request the HttpSession
object, no JSessionID was created. This is different that the normal
behaviour of JSP/Servlets, since even a request to an empty JSP file,
causes a JSessionID to be generated.

/Klaus

mP

unread,
Mar 5, 2007, 12:49:38 AM3/5/07
to Google Web Toolkit
Why isnt a session created with the initial request for the gwt
compiled js pages ?

Sandy McArthur

unread,
Mar 5, 2007, 1:15:59 AM3/5/07
to Google Web Toolkit
On Mar 5, 12:49 am, "mP" <miroslav.poko...@gmail.com> wrote:
> Why isnt a session created with the initial request for the gwt
> compiled js pages ?

Servlet sessions are not established by the server unless the page
requires it. Most static pages do not need a session and if a user
hits index.html first then there is no need for a session.

KlausG,
Can you convert your index.html or whatever your GWT host page is into
a .jsp? Jsp's establish sessions by default which should solve your
problem.

Message has been deleted

Adrian Bosworth

unread,
Mar 5, 2007, 2:37:02 AM3/5/07
to Google Web Toolkit
I was also caught out by this issue as my client app makes several
RPCs in 'parallel' when it starts up. As you say, a simple solution
is to make an initial handshake RPC call that establishes an
HttpSession. It's worth mentioning that you should block further
processing until the handshake AsyncCallback returns as there is no
guarantee that what you consider to be the first RPC will actually be
received first by the server.

KlausG

unread,
Mar 5, 2007, 2:39:18 AM3/5/07
to Google Web Toolkit
Hi

You are right. If I turende my index.html into a JSP I should get a
JSessionId back with the very first request.
What suprises my however is that requesting a GWT RemoteServiceServlet
doesn't generate a JSessionID, since requesting an empty JSP page on
Tomcat does, the following request/response is monitored using
ieHttpHeaders:

GET /index.jsp HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel, application/
vnd.ms-powerpoint, application/msword, */*
Accept-Language: en-us,da;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;
SV1; .NET CLR 1.1.4322)
Host: localhost
Connection: Keep-Alive
Cookie: CP=null*; GUEST_LANGUAGE_ID=en_US

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=0FD8CB53ED22CEB275665E7461FB27B3; Path=/
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 0
Date: Mon, 05 Mar 2007 07:14:52 GMT
Server: Apache-Coyote/1.1

Notich that the content-lenght of the index.jsp is 0 (empty file) and
still the container (Tomcat) returneds a JSESIONID.
Since my application has to obtain a clientId from the server when it
starts I will just have this RPC call execute the statement:
getThreadLocalRequest().getSession(true);
this will insure that a HTTPSession (JSESIONID) has been created.

Thanks for the advise
/Klaus

Reply all
Reply to author
Forward
0 new messages