• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic Session Validation

 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to JSP and am trying to get my first project up an running. I thought I'd start with two basic pages: login.jsp and index.jsp. I wanted to write a scriptlet for the head of index.jsp that would redirect to login if the user was not already logged in. I wrote the following code:

This works just fine for the purpose of redirecting the user . After the above scriptlet, I can print the session suerID and password on the index.jsp page without a problem:
Your user name is: <%= session.getAttribute("UID") %> <br>
Your password is: <%= session.getAttribute("password") %>
The problem is that as soon as I leave the index.jsp page, the next page doesn't find session values for UID or password. Is there something I have to do to get these session variables to persist across multiple jsps? I thought that the calls to session.setAttribute() wuld be enough?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your session.setattribute() method is not getting called. it is in the "else" clause - if the redirection is taken place in the "if" clause, the "else" would never be called and your session variables will never be set.
I hope i have understood what you are trying to do here.
- Amit.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the comments. I think that setAttribute() is getting called. On the same page, but below the scriptlet in question, I have the JSP print out the session attributes and they are populated with the request values.
I don't think that the calls to setAttribute() being inside the else caluse is the problem here. The logic of the scriptlet is basicly:

I'm redirecting because I don't want the user to get to this page unless he is supplying a user id and password (as part of the request from the login page).
Thanks, and please let me know if you see anything else that strikes you as odd.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some Other Tip.

if ( sessionUserName == null || sessionUserName.equals("") ) {
==> I Think. that is not Good.

if ( sessionUserName == null || sessionUserName.length() < 0 ) {
OR
if ( sessionUserName == null || "".equals(sessionUserName) ) {
==> When String compare. if compared String is null is cause NullPointer Exception.
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic