wood burning stoves 2.0*
The moose likes Struts and the fly likes HttpSession equals null in Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "HttpSession equals null in Action class" Watch "HttpSession equals null in Action class" New topic
Author

HttpSession equals null in Action class

Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
Hi guys,

I've got a Struts/Servlet question. Since I'm using the Struts framework in my application, I'm posting this question in the Struts forum.

I want to check whether a HttpSession is invalidated, timed-out or equal to null. When one of these three cases happen, I want my application to redirect to the first page.

In my action classes, I always check if the session equals null by using the following method:

Every time, even when the session is timed-out, it doesn't equal null! Because of that, I can't redirect to the first page...

Is there a way to check whether a session is invalidated or equal to null in my Action classes?

Thanks!


SCJP 1.5, SCWCD 1.4, SCBCD 5
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
There is no way getSession(false) returns an object reference when the session has actually expired. Double check if the session has really expired. You can write a session listener to monitor the state of active sessions. You make want to take a look at HttpSessionListner interface to monitor the active sessions. First start with loggin the session max inactive time out if this doesn't provide the clue then think about implementing Listner.
[ August 16, 2006: Message edited by: Purushothaman Thambu ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Tim,

The real problem here is the fact that a request always goes to the Struts ActoinServlet before it gets to your Action. When Struts processes a request, it always creates a session if one doesn't already exist. So, you're not going to be able to check whether a session has timed out in this way.

The best way to check whether the session has timed out is to place some object in the session such as a User bean or even a simple String that you always expect to be there. In your Action class, check to see if the object exists in session context like this:

User user = (User)request.getSession().getAttribute("user");
if (user == null) {
// the session has timed out
}

If the object is not there, you know the session has timed out.


Merrill
Consultant, Sima Solutions
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
This is something I overlooked until now. After Merrill's post I looked into the source and Struts indeed creates session (but not always)
-The ActionForm associated with the request is session scope
-Processing locale hasn't been disabled.

Or am I missing something?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Purushothaman,

Thanks for keeping me honest. I'll revise my "always" to "most of the time". The processLocale method of RequestProcessor does create a session unless you've disabled automatic loacale setting, and I expect it would be quite unusual for this to be turned off.

Whether Struts creates a session always or most of the time, the point is that checking for a timed-out session with

request.getSession(false)

is unreliable in a Struts application, and one must use another method.
Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
Ok, now I see! I didn't understand how the session could already exsist in my first accessed Action class. I've got plenty of objects in the session, so the necessary check won't be a problem.

This explanation has been very useful to me. Thanks guys!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HttpSession equals null in Action class
 
Similar Threads
Detecting expired sessions
Session expiration in struts using nested tags
problem in session tracking inside my request processor....
Session Timed out Problem
handling session time out