Answer the question
In order to leave comments, you need to log in
How to properly logout a user?
Hello.
I'm trying to organize logout for the user
JSP code
<c:set var="Solution" value="Выход"/>
<LI><a href="<%=request.getContextPath()%>/LogoutUser">${Solution}</a>
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LogoutServlet
*/
@WebServlet(
name = "LogoutUser",
description = "Разрыв сессии пользователя",
urlPatterns = "/LogoutUser"
)
public class LogoutUser extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie cookie : cookies){
if(cookie.getName().equals("JSESSIONID")){
System.out.println("JSESSIONID="+cookie.getValue());
break;
}
}
}
//invalidate the session if exists
HttpSession session = request.getSession(false);
System.out.println("User="+session.getAttribute("user"));
if(session != null){
session.invalidate();
}
response.sendRedirect("login.jsp");
}
}
HTTP Status 405 - HTTP method GET is not supported by this URL
type Status report
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
Answer the question
In order to leave comments, you need to log in
When you follow the link, the GET method is executed, and in your servlet, only the POST method is processed. The easiest, you can rename doPost to doGet
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question