码迷,mamicode.com
首页 > 其他好文 > 详细

案例33-用户退出功能

时间:2018-02-15 10:34:12      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:rect   doget   设置   response   cookie   col   就是   代码   oge   

1 LogoutServlet代码

package www.test.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
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;

public class LogoutServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        //退出的实质就是从session中将user删除
        session.removeAttribute("user");
        
        // 将存储在客户端的cookie删除
        Cookie cookie_username = new Cookie("cookie_username", "");
        Cookie cookie_password = new Cookie("cookie_password", "");
        // 设置 cookie 的持久化时间
        cookie_username.setMaxAge(0);
        cookie_password.setMaxAge(0);
        // 设置 cookie 的携带路径
        cookie_username.setPath(request.getContextPath());
        cookie_password.setPath(request.getContextPath());
        // 发送 cookie
        response.addCookie(cookie_username);
        response.addCookie(cookie_password);
        
        //转发到登录页面
        response.sendRedirect(request.getContextPath()+"/login.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

 

案例33-用户退出功能

标签:rect   doget   设置   response   cookie   col   就是   代码   oge   

原文地址:https://www.cnblogs.com/jepson6669/p/8449251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!