Get your CodeRanch badge!*
The moose likes Servlets and the fly likes Questions about session management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Questions about session management" Watch "Questions about session management" New topic
Author

Questions about session management

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

I was going over session management using the java API and I came down to two examples

one example is from the Head First Servlets and JSP this uses session.isNew
Example is


and the other example i came across is which does not use .isNew method but instead depends on a variable value in the session :



1- I wanted to know which method is better the .isNew one or the other one.
2- Since both the above methods use the java API for server management.. do people still write custom cookies to manage their session ??

Don’t look where you fall, but where you slipped
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
As you said, the first one will always return true, unless you call it with a 'false' parameter. While the second one checks if it's the first visit from the client. These do different things.

As for who using cookies for session management, i am not surr. The API actually uses cookies achieve session management.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bosun Bello wrote:As you said, the first one will always return true, unless you call it with a 'false' parameter. While the second one checks if it's the first visit from the client. These do different things.

As for who using cookies for session management, i am not sure. The API actually uses cookies achieve session management.

I agree that the second one is a counter which basically counts the no of times a user visits that specific address.
But i believe it can also be used for the same purpose as the first code.
If the value of access count > 0 then that means the user is not a new user.


Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 359
accesscount can easily be nulled by another servlet while not influencing the session. isNew method will only return true if there was no session before the corresponding call to getSession.

Checking whether a session already existed can be done using either isNew() or getSession(false) and a null check.


Oracle Certified Professional: Java SE 6 Programmer
Oracle Certified Expert: Java EE 6 Web Component Developer
Oracle Certified Expert: Java EE 6 Enterprise JavaBeans Developer
Jeff Ishar
Greenhorn

Joined: Jul 04, 2011
Posts: 13
Or you can use this



Programmer do stuff in different ways so what you need to find out is what minimize the code and easy to read. And always try to reuse the code which is already done for you unless you want extra functionality like in accessCounter.

Hope it helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions about session management
 
Similar Threads
session tracking
setMaxInactiveInterval(-1)
problem executing the servlet
Jsp Sessions
Session Tracking with cookies disabled.