• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpSession equals null in Action class

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
reply
    Bookmark Topic Watch Topic
  • New Topic