File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes How to check if session exists? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to check if session exists?" Watch "How to check if session exists?" New topic

How to check if session exists?

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172

I've written this code which (i honestly know) is completely inefficient , but anyway... i'm trying to use sessions to determine whether a user is logged in or out... but my super inefficient code isn't doing what i want

here it is..

When i click on the logoff link, it takes me back to the same option to logoff! and thus it is not recognising that the object has been invalidated when the link was clicked! (although i've tested it to see if it is executing the logoff else statement and it was!)
and so if the session has indeed been invalidated, should itbe executing this else statement

Thanks for trying really hard to understand my code
[ December 03, 2007: Message edited by: H Melua ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

I've found it's a lot simpler to test for the existence of an object bound to session than to test for the session itself.

JSPs, by default, create a session so I just assume that there is one and only use getSession() (no arguments) to obtain a reference to it.

Also, these days, it is recommended that this type of logic be factored back into a plain old Java object and controlled from a servlet. Life gets easier once you've moved all of your logic out of the JSPs.
[ December 03, 2007: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
:shocked: :shocked: :shocked:

oh my goodness!!! that workssssss

I should've asked days ago!!!

Thank you "VERY" much, that really drove me nuts!! I thought if i do it this way after i've invalidated the session, it will throw an exception!

Some kind 1 in this forum has advised me to use servlets before, but i'm not getting time to read about them, the course i'm doing is really intensive!!

oh well, life is dragging, and again
thank you very much.................
[ December 03, 2007: Message edited by: H Melua ]
Nguyen Duy Lam

Joined: Mar 08, 2008
Posts: 1
thanks for this post, it working now...
I was looking for a long time..nice job
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: How to check if session exists?