954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

jsp sessions and scriptlet

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.

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


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

Doops
Newbie Poster
7 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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

<c:choose>
   <c:when test="not empty session.loggedUser">
  ...
   </c:when>
   <c:otherwise>
  ...
   </c:otherwise>
</c:choose>


Makes it immediately apparent what you're trying to do, and will not result in an NPE.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You