2013-02-15 97 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