Scriptlet阻止网站启动

时间:2013-02-15 23:13:40

标签: jsp jsp-tags scriplets

我的index.jsp上有一个scriplet,scriplet应该做的是从“session.getAttribute”获取信息并在div上显示,但是即使没有用户登录,index.jsp仍然应该运行

继承人的行踪

<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:%>

我得到的日志说错误“消息在第43行处理JSP页面/Index.jsp时发生异常”

1 个答案:

答案 0 :(得分: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>
相关问题