Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange problem using jsp and servlet

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to find out if cookies are enabled or not in a client's browser.for this what i am doing is, first in one of the jsp pages i set up a cookie and then when the user clicks on the submit button he is forwarded to a servlet.this servlet checks for existence of the cookie set by the jsp page.if the cookie is there then the browser supports cookies otherwise not.my problem comes in here.
in the servlet when i try getting a session using
"HttpSession session = req.getSession(true);"
i am able to find out if the browser supports cookies.but on the other hand if i use
HttpSession session = req.getSession(false);
i dont get any output from the servlet.my question is, since the jsp page has already set up a session, why doesn't the second method works?
any help would be greatly appreciated.
Subbu
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Subbu,
HttpSession is to be used as an alternative for cookies.If you are not getting a HttpSession object with req.getSession(false), then probably the JSP from where you forwarded the request to the servlet didn't create a session instance.
Hope this helps,
Sandeep

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sandeep,
can u think of any reason as to why the session is not set up in the jsp page.i am establishing a session using
session = request.getSession(true);
in the jsp page.
thanks in advance.
Subbu
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Subbu Aswathanarayan:
can u think of any reason as to why the session is not set up in the jsp page.i am establishing a session using
session = request.getSession(true);

This is possible only if in the page directive you have set the session=false.This would mean the page will not have session data.
However, if you have not set this, implicitly JSP engine will create this object for you and you would be getting it when you call the request.getSession(true).
Hope this helps,
Sandeep
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic