Checking Session ID

pwilson
pwilson used Ask the Experts™
on
I've got a list of session Id's, what I'm now trying to do is check each session Id and see if it's still a valid session .

Is this possible?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018

Commented:
 I'd assume that all sessions are still valid, since if they were invalidated/invalid you would not be able to retrieve them.

  Invalidated/deleting a session should clear up the current session object (actually make it elligible for gc).
Mick BarryJava Developer
Top Expert 2010

Commented:
You could in early version of the servlet API (<2.1) using the HttpServletContext method getSession().
But this has been removed and it is no longer possible for security reasons.
>>I'd assume that all sessions are still valid, since if they were invalidated/invalid you would not be able to retrieve them.
he might have manually stored all sessions into a vector or so.

there is no way you can find out if a session is valid. actually you should really not store any session reference in your own code. because session might be passivate/activate and clustered.

I would suggest you to look into SessionListenerS and reconsider your design. for example, HttpSessionListener notify you when a session is created and destoryed:

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSessionListener.html
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Hi,
try this ...

When session is created set a dummy session variable for each session e.g. userId

Now, for checking whether session is valid or not just write -

if(session.getValue("userId")!=null)
{
....
}

In this case, you may not require sessionIds if you are using them for this purpose only.

vaibhav
getValue(java.lang.String name)
          Deprecated. As of Version 2.2, this method is replaced by getAttribute(java.lang.String).
Distinguished Expert 2018

Commented:
> he might have manually stored all sessions into a vector or so.

  True even though I see no point in doing so. Session variables already consume memory, storing them into a container will simply consume more memory.
>> even though I see no point in doing so
that's also my point. there are tons of reason for not doing so, yours and mines

I would say what pwilson is trying to might be not practical and looks like he has no interest to follow up. otherwise i'd like to understand more on his real requirement.

Commented:
check to see which session variables are currently set and compare them against your list:

Vector vs = new Vector();
Enumeration enum= session.getAttributeNames();
String str = "";
String val = "";
out.print("<br>Session variables that are set:<ul>");
     while(enum.hasMoreElements()){
          val = enum.nextElement().toString();
          vs.addElement(val);
          str+=val + "<br>";
        }
out.print(str + "</ul>");

if(vs.contains("your session name")) out.print("ok");

Author

Commented:
Thanks for the comments, sorry for not getting back sooner, decided on going another route.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept kennethxu's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

krispols
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial