How to check if is user logged in?

前端 未结 3 1868
不思量自难忘°
不思量自难忘° 2020-12-05 16:56

I want do display a login link when the user isn\'t logged in and a logout link when the user is logged in. I\'m using container managed security as defined in web.xml

3条回答
  •  长情又很酷
    2020-12-05 17:23

    You may check session to know whether one is logged in or not (if you are using session to manage login information). Assuming you stored user information with the key user,here is an example:

    <%
        String page = "login.jsp";
        String linkName = "Login";
    
        if (session.getAttribute("user") != null) {
            page = "logout.jsp";
            linkName = "Logout";
        }
    %>
    
    <%=linkName %>
    

提交回复
热议问题