Solved

Using Httpsession reference after session-timout?

Posted on 2001-06-13
4
310 Views
Last Modified: 2008-03-06
I have a servlet class, which has a static hashtable. Whenever each request comes, i put the Httpsession reference in the hashtable. Under certain circumstances, I would make use of the Httpsession references to invalidate some session.

My question is, after the session timeout period, will the Httpsession reference be garbage collected? If so, then will the HttpSession reference be null? Or I will access some 'forbidden' area of the memory?
0
Comment
Question by:syyung6
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6185522
As long as you have a reference to the session object it will not be grabage collected
and the reference is still valid.
But perhaps it will throw some exception if you want to access it.
If you use WeakReference for storing the reference to the session it will be garbag
collected and you will be able to track this with ReferenceQueue.
0
 

Author Comment

by:syyung6
ID: 6189083
Could you please explain more about WeakReference?
0
 
LVL 3

Accepted Solution

by:
superschlonz earned 200 total points
ID: 6196177
Since JDK1.2 ther is a package java.lang.ref where you find the class Weakreference.

Normally when you have a reference to an object it will not be garbage collected.
So if you want a reference to an object but also want it to be grabge collected if no
one else uses
If you can use a WeakReference.If you have a reference to your session object you
can just create a new WeakReference with the session object as parameter:

WeakReference wref = new WeakReference( session );

The you add this weak reference to your hash table.
With the get method of Reference you can get the object or if it returns null the object
has been garbage collected.

If you just want to find out when a session times out you can also use the setValue and
getValue methods of HttpSession. If the value object implements HttpSessionBindingListener
it will be notified if it is removed from the session.
I'm not sure but I think that the value will be removed when the session times out.
0
 

Author Comment

by:syyung6
ID: 6200759
Thank you for your help!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now