my dog learned polymorphism
The moose likes Servlets and the fly likes JSP session in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP session in a servlet" Watch "JSP session in a servlet" New topic

JSP session in a servlet

mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
How do I access a Jsp session in a servlet. I used
HTTPRequest.getsession(), but I am not able to access the values
Expecting an reply at the earliest.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If you use
request.getSession() ; // request is a HttpServletRequest
you will get a new empty session if one does not already exist.
Sounds like this is what is happening - try to figure out why the session created in the JSP is not being associated with the servlet.

author of:
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi Zafer,
If u want to use sessions in ur jsp page, here are the few things you have to know.
In jsp, session, is an implicit object, that is it will be there by default.
When ever you are writing a jsp page include session="true" in the page tag like this.
<%@ page language="java" session="true"%>
after that when ever you want to retrieve a value from the session use the following syntax.
HttpSession session=request.getSession(true);
HttpSession session=request.getSession();
The benefit of using the first method is that, if there is no session exists already, it will create a new session and will continue with other pages.
But remember to add session="true" attribute in page tag.
I hope u find this info useful.
I agree. Here's the link:
subject: JSP session in a servlet
It's not a secret anymore!