jsp sessions and scriptlet

Reply

Join Date: Mar 2006
Posts: 7
Reputation: Doops is an unknown quantity at this point 
Solved Threads: 0
Doops's Avatar
Doops Doops is offline Offline
Newbie Poster

jsp sessions and scriptlet

 
0
  #1
Feb 17th, 2007
Hey, I'm getting so fustrated with this now!! I'm making a site in jsp and I have created the registration page and the login page and everything works fine. However I want to start to use the session login to personalise the page depending if they have logged in or not. I thought I had done this using the following code as it worked fine once you had logged in but if you went to the page without logging in it throws a internal server error print stack trace error 500 with a NullPointerException.

  1.  
  2. <% if (sess.getAttribute("loggedUser").toString() == null) { %>
  3. <jsp:include page="\LoggedSessionMenu.jsp" />
  4. <% } else { %>
  5. <jsp:include page="NonSessionMenu.jsp" />
  6. <% } %>

I'm guessing its because the session is empty but I tried with an if statement to test if it is null and this didn't work either.
Any help would be awesome as I'm going mad trying to fix this!
Cheers
Doops
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: jsp sessions and scriptlet

 
0
  #2
Feb 17th, 2007
The string you get back from the session is null, so your attempt to call toString() on it results in an NPE.
You should really use JSTL as well.
Something like
  1. <c:choose>
  2. <c:when test="not empty session.loggedUser">
  3. ...
  4. </c:when>
  5. <c:otherwise>
  6. ...
  7. </c:otherwise>
  8. </c:choose>

Makes it immediately apparent what you're trying to do, and will not result in an NPE.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JSP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC