I am new in this area. I was wondering if only calling "request.getSession(true)
" in java servlet is enough to start session tracking. What about adding JsessionID
in cookie c, setting path for c and response.addCookie(c)
steps before redirecting response to next jsp.
Asked
Active
Viewed 1,036 times
0

Tomasz Nurkiewicz
- 334,321
- 69
- 703
- 674

mrig
- 382
- 1
- 4
- 21
1 Answers
1
Servlet container will do all of this for you. Just call getSession(true)
and the JSESSIONID cookie is added to the next response (providing that response headers were not yet sent). Actually, servlet container tries to abstract you from implementation details and just provides HTTP session abstraction.
Also by default every JSP file creates session once accessed. Extra care must be taken when server does not support cookies, URL-rewriting must be taken into account when rendering URLs.
See also

Community
- 1
- 1

Tomasz Nurkiewicz
- 334,321
- 69
- 703
- 674
-
This link was helpful. It would be great if someone could explain me following or point me to link related to this. At server side after creating session, cookie value names are __utma and __utmz and somehow session tracking was not working. After response.addCookie(c), where c is('JSESSIONID', session.getId()) cookie value names are same though session tracking starts to work. I am not able to comprehend this. – mrig Oct 28 '12 at 06:28
-
@mrig: these cookies come from Google Analytics (http://stackoverflow.com/questions/10626196), they have nothing to do with servlet session. And again, you should **never** set/clear `JSESSIONID` cookie manually. It's done by the container. – Tomasz Nurkiewicz Oct 28 '12 at 07:43
-
okay, so something weird is happening at the college server which I will discuss with them. The session is regenerated every time I visit jsp page from java servlet (after generating session), if I don't do response.addCookie(c) for JSESSIONID. Could this be some setting in tomcat6.0 – mrig Oct 28 '12 at 19:15