Solved

Using Httpsession reference after session-timout?

Posted on 2001-06-13
4
317 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

862 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

21 Experts available now in Live!

Get 1:1 Help Now