This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes HttpSession.invalidate() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpSession.invalidate() method " Watch "HttpSession.invalidate() method " New topic
Author

HttpSession.invalidate() method

kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
After calling HttpSession.invalidate() method, what will be returned when we try to call getServletContext(), getMaxInactiveInterval() method which will not throw IllegalStateException exception like any other methods.

Do they retrun null.


krad
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by kranthi adari:
which will not throw IllegalStateException exception like any other methods.


getServletContext() . i dont think , it is difficult to test yourself
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

This would be extremely easy to test. Please let us know what you discover.
[ January 01, 2009: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
After calling HttpSession.invalidate() method,I called getServletContext(). There is no exception thrown. I tried to print context Object returned as well as session object before and after calling invalidate() method.
It is printing correctly.

But is this the expected behavior.
After the session is invalidated, how am I getting servletContext object instead of a null.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

Why would you think that the session would have any effect whatsoever on the servlet context?
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Thank you. Nice question. I got the answer too now.
ServletContext is not only tied to a single client session. So, obviously it should not get effected when the session is invalidated. Similary getMaxInactiveInterval() will just give maximum inactive time a session can be. These two functions doesnot have anything to do with session validity or invalidiy.

Please correct me if I am wrong!!

But what about setMaxInactiveTime() which has to do with the current session which is already invalidated. I will test and revert back.
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
The method is getMaxInactiveInterval() but not getMaxInactiveTime().

After invalidaing method, I am still able to call set and get MaxInactiveInterval() method. I also observed that the changed maxInactiveInterval is updated on the invalidated session. Can any one tell me why?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by kranthi adari:
I also observed that the changed maxInactiveInterval is updated on the invalidated session


No,I tested . but dont find this behaviour.
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
Following is the code snippet I wrote in my servlet in which session object maxInactiveInterval is updated to 40mins even through it is invalidated. In the web.xml <session-timeout> is set to 30mins.
------------------------------------------
//code begins:

//code ended.

In the output it is showing that maxInactiveInterval is set to 40 mins.


William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
Examining the API, I see that the methods which throw IllegalStateException are those which set or retrieve attributes. I suppose the developers didn't consider it necessary to throw an exception for other methods since there is no danger involved in using those methods on an invalid session.

Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HttpSession.invalidate() method