2012-10-11 76 views
2

我是java中的新手。無法訪問會話屬性

我想在servlet中設置一個屬性到會話,然後在兩個不同的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我: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

在這行你得到的NPE? 'sList'定義在哪裏? – alfasin

+0

我想這是因爲你使用了未定義的屬性「sList」和「sStudt」 –

回答

0

試着改變你的C:如果條件檢查,如果變量在會話中可用如下

<c:if test="${not empty sessionScope.sStudt}"> 

我想應該理清您的問題

+0

謝謝all.I解決了我的問題。 – user1683982