在学习jsp在top menu bar上构建登录/注销时,我在这个论坛中使用了类似的question。我想知道使用session的方法是相同的,这个session对象应该在LoginServlet中还是在SessionServlet中来控制这种UI行为,如果我在session中使用MongoDB,我如何实现这个结果?
发布于 2020-09-30 06:36:59
这取决于您的登录/注销将通向何处。在大计划中,这真的无关紧要。例如,如果您选择将其定向到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 uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>https://stackoverflow.com/questions/64118697
复制相似问题