在哪里包含session.invalidate()代码



在我的JSP页面中,我有注销按钮。 当我单击它时,我应该调用一个 servlet 并包含 session.invalidate() 代码或调用 JSP 并包含此代码或将代码包含在我有注销按钮的每个 JSP 页面中。

注销.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%
        session.invalidate();
        response.sendRedirect("index.html");
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>logout</title>
    <%     
    response.addHeader("Cache-Control", "no-cache,no-store,private,must-revalidate,max-stale=0,post-check=0,pre-check=0"); 
    response.addHeader("Pragma", "no-cache"); 
    response.addDateHeader ("Expires", 0);
    %>
</head>
<body>
</body>
</html>

您只有一个注销页面。您可以在 Web 应用程序的每个页面上都有一个注销按钮。但是,一旦您从任何页面单击注销按钮,您应该会被重定向到您的注销.jsp文件。

yOU可以制作页眉和页脚页面。在这里,您可以添加注销按钮。您可以将此页面包含在需要注销按钮的所有其他 jsp 页面上,而不是在每个页面上编写注销按钮代码。

最新更新