N
N
Nikolay Baranenko2016-06-23 17:39:21
Java
Nikolay Baranenko, 2016-06-23 17:39:21

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>


JAVA servlet code

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");
    }

}


Browser swears by an error
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.


Please help me where is the error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Smith, 2016-06-23
@drno-reg

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 question

Ask a Question

731 491 924 answers to any question