This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Session Validation

 
Dave Wingate
Ranch Hand
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Amit Delve
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
jhong jaewon
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic