Solved

Using Httpsession reference after session-timout?

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

770 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