Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking session  RSS feed

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

What is the best way to check if a session exists in a jsp using jstl ?
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that using JSTL(or EL) one can access the variables in session or request scope.
And a session would always be associated with a JSP(or any Request, for that matter). I assume that what you want to check is existence of a variable in session scope. That can be done using EL.

Cheers,
Sid
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, each JSP page has valid session object exits, because of page directive.
But If you make it false, then with JSTL we can do something like this,



I might be wrong here, please correct me If so


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:

I might be wrong here, please correct me If so


Think of this ${sessionScope == null} ?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:

Think of this ${sessionScope == null} ?


Hmm, After looking at JSP docs, I found that "sessionScope" and "seesion" are different object, first one is Map whereas later one is actual session object,

So I got a session object,



Now , question arises about how to check session existence, thus I checked its nullabilty, like



But, it didn't worked out, because 'null' is a literal and not an object

So I found something new, which is valid for any object/values, empty operator,
So finally it works !!,



Is it correct ?
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of the suggestions work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you actually trying to accomplish?
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make sure a session exists on my page, if it doesnt the user is redirected to the login page.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why? Checking for the session itself is really, really weird.

Are you just trying to find out if someone is logged in or not? That's usually done by checking for specific scoped variables in the session, not the session itself.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for enlightening me. So if I have the following var in my session:



How would I check it in a jsp file ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I would do this in a filter so that you don;t have to check in each and every JSP (and are you not using page controllers?)

But tio check for the existence of a scoped variable in session scope:


 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I am using controllers for all my pages except for the index (its an ajax app). What is a filter ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have page controllers, why would you be waiting until you get to the JSP to perform this kind of check?

Servlet filter

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I looked into the filter and it looks very promising, I am planning on scrapping my current layout and use filters instead, but us newbies get out of one hole only to fall into another. Having said that, here is a thread I could use your assistance on: http://www.coderanch.com/t/434266/Servlets/java/Filter#1929582
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!