簡體   English   中英

如何在Java jsp中動態更改登錄和注銷鏈接?

[英]How to change login and logout links dynamically in Java jsp?

我在這個論壇上有這個類似的問題,同時學習 jsp 在top menu bar上構建登錄/注銷。 我想知道使用 session 的方式是否相同,這個 session 對象應該在LoginServle t 或SessionServlet來控制這個 UI 行為,如果我在 session 中使用 MongoDB,我如何實現這個結果?

這取決於您的登錄/注銷將導致何處。 在宏偉的計划中,這真的無關緊要。 例如,如果您選擇將它定向到 LoginServlet,您將像這樣處理它:

HttpSession session = request.getSession();
if(session.getAttribute("ATTRIBUTE")==null){//Make sure that when the user logs in, you set the attribute
    //They are not logged in
    response.sendRedirect("Login.jsp");
}else{
    //They are logged in
    response.sendRedirect("Home.jsp");
}

如果要顯示用戶是登錄還是注銷,可以像這樣使用<c:choose>標簽:

<c:choose>
    <c:when test="${sessionScope.username != null}">
        You are logged in!
    </c:when>
    <c:otherwise>
        You are not logged in :(
    </c:otherwise>
</c:choose>

不要忘記重視<head>上方的 taglib :

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

暫無
暫無

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

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