我是java的新手。
我正在尝试在servlet中为session设置一个属性,然后在两个不同的jsps中访问它。但我得到第二个jsp的空指针异常。
我的servlet:
HttpSession session = request.getSession(true);
if (null == session.getAttribute("username")) {
response.sendRedirect("login.jsp");
} else {
EdsStudentForm eds = (EdsStudentForm) form;
try {
List<UserApplication> userList = uaDAO.searchUser(eds);
if (!userList.isEmpty()) {
request.getSession().setAttribute("userList", userList);
action_forward = SRCHSUCCESS;
} else {
action_forward = SRCHFAILURE;
}
} catch (Exception e) {
e.printStackTrace();
}
}
JPS I:sTbl.jsp 我的第一个JSP使用
没有问题 <html:form method="post">
<table border="1" width="400" align="center" class ="sample">
<tr>
<td class ="divheader">Student Code</td>
<td class ="divheader">First Name</td>
<td class ="divheader">Last Name</td>
</tr>
<c:set var ="sStudt" value="${userList}" scope="session" />
<c:forEach items="${userList}" var ="uList">
<tr>
<td class ="divheader"><a href="searchSuccess.jsp?"><c:out value="${uList.studentCode}"></c:out></a></td>
<td class="divheader"><c:out value="${uList.firstName}"></c:out></td>
<td class ="divheader"><c:out value="${uList.lastName}"></c:out></td>
</tr>
</c:forEach>
</html:form>
我的第二个JSP:searchSuccess.jsp
<html:form method="post">
<p class ="sample">Personal Information</p>
<table class ="table1">
<c:if test="${not empty sStudt}">
<c:forEach var="sList" items="${sessionScope.sStudt}">
<tr>
<td>Student Code:</td>
<td><c:out value="${sList.studentCode}"/><td>
</tr>
<tr>
<td>Title:</td>
<td><c:out value="${sList.title}"/></td>
<td>First Name:</td>
<td><c:out value="${sList.firstName}"/></td>
<td>Last Name:</td>
<td><c:out value="${sList.lastName}"/></td>
</tr>
<tr>
<td>Street Number:</td>
<td><c:out value="${sList.streetNumber}"/></td>
<td>Street Name:</td>
<td><c:out value="${sList.streetName}"/></td>
<td>#Suite/Apt.:</td>
<td><c:out value="${sList.suite}"/></td>
</tr>
etc..etc...
我收到了java.lang.NullPointerException
你能告诉我我做错了吗?
提前致谢。
答案 0 :(得分:0)
尝试更改c:if条件以检查变量是否在会话中可用,如下所示
<c:if test="${not empty sessionScope.sStudt}">
我想这应该解决你的问题