Solved

Using Httpsession reference after session-timout?

Posted on 2001-06-13
4
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 236
Weekend adv creator 3 64
login jsp example 24 85
Website checklist for browser compatibility? 2 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

735 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