drno-reg
@drno-reg
см не кратко

Как правильно выполнить Logout пользователя?

Здравствуйте.

Пытаюсь выполнить организовать logout пользователю

JSP код

<c:set var="Solution" value="Выход"/>
					<LI><a href="<%=request.getContextPath()%>/LogoutUser">${Solution}</a>


JAVA код сервлета

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.


Прошу помочь в чем ошибка.
  • Вопрос задан
  • 910 просмотров
Решения вопроса 1
@Braidner
При переходе по ссылке выполняется GET метод, а у вас в сервлете обработка только POST метода. Самое просто, можно переименовать doPost в doGet
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
Bell Integrator Ульяновск
До 400 000 ₽
07 мая 2024, в 18:09
2000 руб./за проект
07 мая 2024, в 17:40
300 руб./за проект