這裏是我的代碼:如何根據會話屬性的值更改包含在JSP頁面中的頭文件?
<%
if(session.getAttribute("loggedIn").equals(null))
{
%>
<%@ include file="header.jsp"%>
<%
}
else if(session.getAttribute("loggedIn").equals("user"))
{
%>
<%@ include file="pheader.jsp"%>
<%
}
%>
這是我如何設置會話屬性:
if(utype.equals("admin"))
{
session.setAttribute("loggedIn", "admin");
}
else
{
session.setAttribute("loggedIn", "user");
}
這是拋出一個空指針異常。 我明白,因爲沒有人登錄,屬性值爲空。
我該如何解決這個問題?
這可能有幫助 - http://stackoverflow.com/questions/4988086/include-file-from-dynamic-property-value –
採取一個變量,如果會話等於空或第一個條件爲真然後設置'標題。 jsp'中的變量,如果會話不等於null或第二個條件爲真,則在變量中設置'pheader.jsp'。並像這樣使用 - ..(在這兩種情況下使用此jsp:include ...行)。 –
但是,NullPointerException在'if(..)'部分。 @PuneetChawla – Nivedita