注销servlet不起作用:注销后姓氏和注销按钮仍然可见

问题描述 投票:1回答:1

我有一个注销servlet,它似乎不起作用。在我转到/logout页面后,它确实会重定向回/home,但是用户的名字和姓氏以及“注销”按钮仍然存在:

注销前:

enter image description here

注销后:

enter image description here

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>
java jsp session servlets
1个回答
0
投票

我忘了放

@WebServlet("/logout")

LogoutServlet类的顶部。

© www.soinside.com 2019 - 2024. All rights reserved.