2013-02-15 96 views
0

我有我的index.jsp一個scriplet,什麼scriplet應該做的是擺脫「session.getAttribute」關於DIV的信息和展示,但仍的index.jsp應該即使沒有用戶運行被記錄在Scriplet停止網站從開始

繼承人的scriplet

<div class="templatemo_content_left_section"> 
40: <h1>Bem Vindo</h1> 

40: <%= session.getAttribute("currentSessionUser")%> 
41: <%if (session.getAttribute("currentSessionUser").equals("")){%> 
42: <a href="Login.jsp"><b>Login</b></a> 
43:<%} 
44: else{%> 
45:<a href="logout.jsp"><b>Logout</b></a> 
46:<% 
47:} 
48:%> 

日誌我得到說錯誤「消息時處理JSP頁面/Index.jsp例外,在第43行」

回答

0

session.getAttribute(),則返回null屬性不存在。那是documented explicitely。所以很明顯,如果你調用的結果equals(),你會得到一個NullPointerException。將結果與null進行比較:

<%if (session.getAttribute("currentSessionUser") == null) 

甚至更​​好,使用JSP EL和JSTL。 Scriptlet應該避免:

<c:choose> 
    <c:when test="${empty sessionScope.currentSessionUser}"> 
     <a href="Login.jsp"><b>Login</b></a> 
    </c:when> 
    <c:otherwise> 
     <a href="logout.jsp"><b>Logout</b></a> 
    </c:otherwise> 
</c:choose> 
+0

感謝您的快速回答。學不好的還是舊的教學方式,但現在我有一個問題,它返回什麼是「[email protected]」,而不是用戶名,有任何想法如何解決呢? – HugoMonteiro 2013-02-15 23:45:08

+0

這意味着你有類型的對象login.UserBean存儲在此會話屬性。從該對象獲得的名稱:'的UserBean的UserBean =(的UserBean)session.getAttribute( 「currentSessionUser」); String userName = userBean.getName();'。 EL:'$ {sessionScope.currentSessionUser.name}'會容易得多。請問你的老師教你12年多沒有過時的事情。 – 2013-02-15 23:47:42

+0

像其他正確的腳本里面? – HugoMonteiro 2013-02-15 23:49:40