Create a JSP Logout Link

when a user logs into my application, he submits the form to be processed through the Servlet. The servlet creates a session for the user. How to create a link so that the user can log out? It seems I cannot directly reference the servlet. How to delete a session and a link to a home page?

Here is how I can do this, but it does not seem to be "right." Could I reference index.jsp? Logout = true. My index.jsp will see if logout is true and delete sessions.

Is there any other way to do this?

+4
source share
5 answers

Write a servlet mapped to /logout , which then does something like this in doGet :

 HttpSession session = request.getSession(false); if(session != null) session.invalidate(); request.getRequestDispatcher("/index.jsp").forward(request,response); 

It doesn't matter if the user has a session or not, they will eventually be redirected to index.jsp .

+9
source

It was easier for me to do this:

 <form method="link" action="logout.jsp"> <input type="submit" value="Logout"/> </form> 

without logout.jsp having this:

 <% session.invalidate(); response.sendRedirect("startpage.html"); %> 
+5
source

Based on cdietschrun's answer, I made it even more compact:

 <% session.invalidate(); response.sendRedirect(request.getContextPath()); %> 
+1
source

The output is not too serious. you can have a simple /logout.jsp just to end the session.

0
source

The easiest way to do this is to link to the exit in this way.

 <a href="logout.jsp">LogOut</a> 

And in "logout.jsp" write below code

 <% session.invalidate(); response.sendRedirect("index.jsp"); %> 
0
source

Source: https://habr.com/ru/post/1342218/


All Articles