1

I am using this code to logout but still after logout I am able to hit url. Logout seems not working, I am calling this class when clicking on Logout image. Please help. Thanks in advance.

public class LogoutServlet extends HttpServlet {

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) {

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(PermissionDatabase.cookieName)) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
        Redirect.sendToLogin(response);
    }
p05p
  • 120
  • 8
  • 1
    Possible duplicate of [How to properly logout of a Java EE 6 Web Application after logging in](https://stackoverflow.com/questions/10893727/how-to-properly-logout-of-a-java-ee-6-web-application-after-logging-in) – Arnaud Mar 06 '18 at 13:42
  • you are missing session invalidate and hit URL (as u mentioned i didn't understood what happening next) might be browser cache problem. So clear the browser cache. – mallikarjun Mar 06 '18 at 13:43

2 Answers2

1
request.getSession().invalidate();
D. Ch
  • 36
  • 3
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Thomas Flinkow Mar 06 '18 at 14:17
0

Can you, please try these.

1st :


 Cookie cookie=new Cookie("nameOfCookie","");  
    cookie.setMaxAge(0);  
    response.addCookie(cookie);



2nd :   

     for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("nameOfCookie")) {
                        cookie.setMaxAge(0);
                        cookie.setValue("");

                        response.addCookie(cookie);
                    }
                }