[英]How to check in JSP if user is logged (using JSTL <if> statement), and display username?
我知道在SO上有許多類似的問題 - 大多數都是指用servlet過濾器實現這個概念。
我使用基於表單的聲明式安全方法。 並且,當一些用戶在登錄表單上輸入他(或她)的憑證(用戶名和密碼)時,他被重定向到某個/安全的jsp頁面。 該頁面內容的一部分是:
Hello <%=request.getUserPrincipal().getName().toString()%>
You are able to view this page because you are authorized user.
現在,如果用戶已登錄,我想顯示他/她的用戶名,無論是否有安全的jsp頁面。 像這樣的東西:
<c:if test="${statement that checks if some user is logged in}">
User: ${username of the logged user here}
</c:if>
編輯我還想用適當的JSTL命令替換JSP scriptlet部分: <%=request.getUserPrincipal().getName().toString()%>
。
<c:if test="${not empty pageContext.request.userPrincipal}">
User: <c:out value="${pageContext.request.userPrincipal.name}" />
</c:if>
使用c:out
正確地轉義打印值並防止XSS攻擊。
我在兩種情況下都缺少pageContext
對象, request
本身不是JSP頁面中的隱式對象。
<c:if test="${not empty pageContext.request.userPrincipal}">
User <c:out value="${pageContext.request.userPrincipal.name}" />
</c:if>
和<%=request.getUserPrincipal().getName().toString()%>
JSTL等價物是: <c:out value="${pageContext.request.userPrincipal.name}"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.