Reputation: 8001
I've this Register.jsp
page which on page load, I want to check if a user is already logged in. So I've this code,
<%
String usertype = (String)session.getAttribute("usertype");
if(usertype.equals("student")){
response.sendRedirect("studenthome.jsp");
}
else if(usertype.equals("faculty")){
response.sendRedirect("facultyhome.jsp");
}
%>
It works when a user is logged in(it redirects to studenthome.jsp
if usertype
equals student
and redirects to facultyhome.jsp
if usertype
equals faculty
), but when usertype
is null
, I just want to continue loading the page instead I get following error. What am I doing wrong here
Upvotes: 2
Views: 90
Reputation: 46881
Try to avoid Scriplet at all in 21st century instead use JavaServer Pages Standard Tag Library
Try with c:redirect
and c:choose
. Find more details about Oracle Tutorial - Core Tag Library
There is no need to handle NullPointerException
using JSTL (in this case) as shown in below sample code.
Sample code:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:choose>
<c:when test="${usertype == 'student'}">
<c:redirect url="studenthome.jsp" />
</c:when>
<c:when test="${usertype == 'faculty'}">
<c:redirect url="facultyhome.jsp" />
</c:when>
<c:otherwise>
<c:redirect url="home.jsp" />
</c:otherwise>
</c:choose>
Note: remove c:otherwise
if not needed.
Upvotes: 1
Reputation: 21981
When usertype is null, I just want to continue loading the page instead of getting error.
You will get NullPointerException
if usertype
is null
. You can simply ignore this error by swap the position of usertype
on equals
method of if-else
condition
if("student".equals(usertype)){
response.sendRedirect("studenthome.jsp");
}
else if("faculty".equals(usertype)){
response.sendRedirect("facultyhome.jsp");
}
Upvotes: 2