Basic Web application creation Servlet / JSP (logout function)

Introduction

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.

Execution environment

1. Controller for logout processing

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
	}

}

  1. Discard the session scope.
  2. Forward the logout screen

2. Modify the view of the bulletin board screen

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

Create view of logout screen

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

Basic Web application creation Servlet / JSP (logout function)
Basic Web application creation Servlet / JSP (login function)
Basic Web application creation Servlet / JSP (posting screen)
[Spring Boot] Web application creation
Web application development memo with MVN, Tomcat, JSP / Servlet with VScode
Until you create a Web application with Servlet / JSP (Part 1)
Web application creation with Nodejs with Docker
Comparison of WEB application development with Rails and Java Servlet + JSP
A certain programming classroom Ruby basic application function
Personal application creation # 2
Personal application creation # 3
Personal application creation # 1
[Servlet] Basic method