我有一个注销servlet,它似乎不起作用。在我转到/logout
页面后,它确实会重定向回/home
,但是用户的名字和姓氏以及“注销”按钮仍然存在:
注销前:
注销后:
LogoutServlet.java:
public class LogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
HttpSession session = request.getSession(false);
if (session != null) {
session.removeAttribute("user");
session.invalidate();
}
response.sendRedirect(request.getContextPath() +
"/home");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
header.jsp:
<%@ page import="comediansapp.entities.main.User" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<div class = "header-container">
<div class = "header">
<%if(session.getAttribute("user") == null){%>
<div class = "header-buttons">
<a href="${pageContext.request.contextPath}/login">Login</a>
<a href="${pageContext.request.contextPath}/signup">Signup</a>
</div>
<%
} else {%>
<div class="user-email">
<%
User user = (User) session.getAttribute("user");
out.println(user.getFirstname() + " " +
user.getLastname());
%>
</div>
<div class="button logout-button">
Logout
</div>
<%
}
%>
</div>
</div>
home.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:include page="../shared/header.jsp" />
</body>
</html>
我忘了放
@WebServlet("/logout")
在LogoutServlet
类的顶部。