Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about cookie jsessionid

 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When exactly is the JSESSIONID cookie set on the browser?
I access my JSP page from a browser after clearning all of the browsers cookies. The JSP page contains only a text box and a submit button.
As soon as the jsp page loads, I can view the Jsessionid cookie being set on the browser.
Why is this so? I thought the cookie is set only when a response is returned to the client. In this case, I've not returned any response to the client from the servlet. In fact, I have not hit the servlet at all !
Where am I going wrong?
Thanks.
Amit
 
stan ioffe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, (and i could be wrong ), your browser's cookies are disabled, thus, the URL is automatically re-written with an appended jsessionID.

Below is an extract from http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets11.html which is under "Session Tracking".
----
If cookies are turned off, the session is encoded in the Check Out URL as follows:
http://localhost:8080/bookstore1/cashier;
jsessionid=c0o7fszeb1

If cookies are turned on, the URL is simply
http://localhost:8080/bookstore1/cashier
----
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the browsers cookies are enabled. If they were not enabled, I wouldnt see the list of cookies when I click on View Cookies in Firefox.

I see no ?jessionid=xxxxxxxxxx in the URL which means URL writing is not being used.

As soon as I clear all the cookies in my Firefox and hit the JSP page and view the cookie list again, I immediately see a cookie called JSESSIONID.

Cant understand what the hell is going on !!
Wasted an hour on my job figuring this out !!

Anyone got any clue ?
Thanks,
Amit
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you access the JSP in browser, the session attribute of the page directive is true by default. so the session is created and activated by default when you access the page. If you want more testing you can set session attribute to false, so no new session is created or check if session is new in the JSP. I think the session.isNew() method will return always false for default JSP page setting as the session is always established when the page is accessed.

Hope it help you

thanks
 
Amit Saini
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that makes sense. Thanks for your help !
Indeed, session.isNew always kept returning false even when I accessed for the first time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic