I am trying to develop a JavaEE 7.0 web application using NetBeans (IDE), GlassFish 4.1.0 (web server) and MySql 5.7.11 (Database server). My Operating System is Windows 8.1.
I would like to implement a very basic authentication system. The user enters a username and a password already inserted in the database and if it is correct, he will be able to access a webpage called patient.jsp. He is also able to log out from this webpage.Therefore, I proceeded in the following way in order to develop this authentication system:
1- I created three files patient.jsp,loginform.jsp and PatientServlet.java. This is the project hierarchy:
2 - The code is:
For 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);
}
}
For 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>
For 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>
For 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- Therefore, I executed the application and I started the LogIn Form as desired:
4- Then, I obtained the following webpage as wished:
5- However, when I press the LogOut link, the following error appears:
I really do not know what is going on. Could you help me please ?
Thank you so much for your help.
The logout.jsp is not directly accessible from the client since it is located in the WEB-INF directory. You need to move logout.jsp to the root of the web application.