This time, we will continue from the previous Basic Web application creation Servlet / JSP (posting screen). The goal of this article is to implement the ability to destroy session scope and log out.
Logout.java
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
public Logout() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate(); //1
request.getRequestDispatcher("/WEB-INF/view/logout.jsp").forward(request,response); //2
}
}
bulletin-board.jsp
<h2>This is the bulletin board screen.</h2>
<p><c:out value="${loginUser.name}"/>Is logged in.</p>
<a href="./Logout">Log out</a> //Log out Specify the URL to execute
logout.jsp
<p>logged out.</p>
<a href="./login">To the bulletin board screen</a>
The logout function is completed by adding the above.
Recommended Posts