簡體   English   中英

將getAttribute()腳本重寫為JSP / HTML中的JSTL?

[英]Rewriting getAttribute() scriptlet as a JSTL in JSP/HTML?

我的JSP頁面中有一個scriptlet,該scriptlet獲取從先前的serlvet傳遞的屬性“用戶名”。 腳本將驗證是否設置了“用戶名”屬性,如果沒有設置,它將拒絕訪問主頁,而是重定向到登錄頁面:

<%
    String validUser = (String) session.getAttribute("username");
    if (validUser == null){
    session.setAttribute("errorMsg", "Access Denied:  Please login to access this page");
    session.setAttribute("username", "");
    response.sendRedirect("LoginFormError.jsp");
        }
%>  

由於JSP / HTML代碼中的scriptlet不是理想的,我該如何改用JSTL重寫此scriptlet?

編輯

好的,到目前為止,這是我所擁有的:

<c:set var="validUser" value='${param.username}' />

<c:if test = "${validUser == null"}   
   <c:set var="errorMsg" value="${'Access Denied:  Please login to access this page'}"/>
   <c:set var="username" value=""/>
   <c:redirect url="LoginFormError.jsp"/>
</c:if>

getAttribute()和setAttribute()是否正確完成?

很高興看到您這樣做,您可以使用jstl/core taglib

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:if test="${sessionScope.username == null}">
    <c:redirect url="LoginFormError.jsp" />
</c:if>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM