如何在java中编写注销代码

时间:2015-03-27 06:44:31

标签: java servlets logout

这是我的代码:

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       HttpSession session = request.getSession(false);
      if (session != null) {
            session.removeAttribute("FirstName");
            session.removeAttribute("pwd");

            session.invalidate();
      }
           response.sendRedirect("index.html");
    }

    }

在此之后我该怎么办? 任何帮助将不胜感激........

1 个答案:

答案 0 :(得分:0)

我认为最好使用Cookie注销.Code for Cookies是:

Cookie loginCookie = null;
Cookie[] cookies = request.getCookies();
if(cookies != null) {
   for(Cookie cookie : cookies){
      if(cookie.getName().equals("uid")){
         loginCookie = cookie;
         break;
      }
   }
}
if(loginCookie != null){
   loginCookie.setMaxAge(0);
   response.addCookie(loginCookie);
}
response.sendRedirect("index.html");