943,981 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 4422
  • JSP RSS
Feb 17th, 2007
0

jsp sessions and scriptlet

Expand Post »
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.

JSP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Doops is offline Offline
7 posts
since Mar 2006
Feb 17th, 2007
0

Re: jsp sessions and scriptlet

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
JSP Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: how use iframes in a form
Next Thread in JSP Forum Timeline: Connecting JSP to Oracle, problems with table





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC