如何从 jsp 中实现的网页注销 - JavaEE 和 NetBeans

How to logout from a webpage implemented in jsp - JavaEE and NetBeans

我正在尝试使用 NetBeans (IDE)、GlassFish 4.1.0(网络服务器)和 MySql 5.7.11(数据库服务器)开发 JavaEE 7.0 网络应用程序。我的操作系统是 Windows 8.1.

我想实现一个非常基本的身份验证系统。用户输入数据库中已经输入的用户名和密码,如果正确,他将能够访问名为 patient.jsp 的网页。他也可以从这个webpage.Therefore注销,为了开发这个认证系统,我按照以下方式进行:

1- 我创建了三个文件 patient.jsp、loginform.jsp 和 PatientServlet.java。这是项目层次结构:

2 - 代码是:

对于PatientServlet.java:

@WebServlet("/patient")
public class PatientServlet extends HttpServlet {    
private static final long serialVersionUID=1L;

    @override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        this.getServletContext().getRequestDispatcher("/WEB-INF/patient.jsp").forward(request,response);
     }
  }

对于loginform.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
 <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    Sign In:
    <form method="POST" action="j_security_check">
        Username:<br>
        <input type="text" name="j_username" size="20">
        Password:<br>
        <input type="password" name="j_password" size="20">
        <br>
        <input type="submit" value="Sign In">
    </form>
</body>

对于logout.jsp:

<%@ page session="true"%>

User '<%=request.getRemoteUser()%>' has been logged out.
<% session.invalidate(); %>

<br/><br/>
<a href="test">Go to Log In form </a>

对于patient.jsp:

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>

  <%@page contentType="text/html" pageEncoding="UTF-8"%>
  <!DOCTYPE html>
  <html>
     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
     </head>
     <body>
       <h1>Hello!</h1>
       <a href="logout.jsp">Click here to log out </a>        
      </body>
   </html>

3- 因此,我执行了应用程序并根据需要启动了登录表单:

4- 然后,我如愿获得了如下网页:

5- 但是,当我按注销 link 时,出现以下错误:

真不知道怎么回事。你能帮帮我吗?

非常感谢您的帮助。

logout.jsp 无法从客户端直接访问,因为它位于 WEB-INF 目录中。您需要将 logout.jsp 移动到 Web 应用程序的根目录。