R
R
RalfiBB2020-02-18 17:37:24
Java
RalfiBB, 2020-02-18 17:37:24

How to correctly execute Servlet Logout?

How to correctly logout in a servlet so that after logout it is impossible to return "Back" using the browser button? At the moment, the logout servlet looks like this:

// Logout
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        UsersDataSet user = accountService.getUserBySessionId(sessionId);

        if (user == null) {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().println("Unauthorized. (401)");
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        } else {
            accountService.deleteSession(sessionId);
            session.invalidate();
            response.sendRedirect("/logout");
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question