简体   繁体   English

如何在Java jsp中动态更改登录和注销链接?

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

I have this similar question in this forum while learning jsp to build a login/logout on the top menu bar .我在这个论坛上有这个类似的问题,同时学习 jsp 在top menu bar上构建登录/注销。 I wonder is it the same way to use session and should this session object be in LoginServle t or SessionServlet to control this UI behaviour, and how can I achieve this result if I use MongoDB with the session?我想知道使用 session 的方式是否相同,这个 session 对象应该在LoginServle t 或SessionServlet来控制这个 UI 行为,如果我在 session 中使用 MongoDB,我如何实现这个结果?

It depends where your login/logout will lead to.这取决于您的登录/注销将导致何处。 In the grand scheme, it really doesn't matter.在宏伟的计划中,这真的无关紧要。 For example, if you chose to direct it towards LoginServlet, you would handle it like so:例如,如果您选择将它定向到 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");
}

If you want to display whether the user is logged in or logged out, you can use the <c:choose> tag like so:如果要显示用户是登录还是注销,可以像这样使用<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>

Don't forget to important the taglib above the <head> :不要忘记重视<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