The moose likes Servlets and the fly likes a Question about sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "a Question about sessions" Watch "a Question about sessions" New topic

a Question about sessions

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
hey guys, me again !!
I have created a session using
HttpSession session = request.getSession(true).
So far, so good, is working smoothly.
1o.) I'm using a servlet and I need to retrieve some values from the session. I'm doing it this way:
HttpSession session = request.getSession(true)
String someString = session.getAttribute("att")
Am I creating a new session if I do this?? or Am I retrieving the current session (what I actually want to achieve)??
2o.) How do I ask if the session is already created?
using isNew()?
or extracting some value from the session and asking if it is null?

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Mayer Salzer

Joined: Apr 01, 2002
Posts: 20
1) request.getSession(true) retreives the current session if it was already created before otherwise it creates a new one (first time in).
2)There are a number of ways to determine if a session is new. isNew() will work, so will extracting some value from the session and asking if it is null provided you put it into the session before. A third option is checking for request.getSession(false) == null. . .

Sun Certified Programmer for Java 2 Platform
I agree. Here's the link: http://aspose.com/file-tools
subject: a Question about sessions
Similar Threads
Correct way to use getSession() in actions?
Not get the session value
null value in session variable when showing not null in another page!
Session Lost from JSP to action