Cookie的使用类

import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName:CookieUtils
 * @Description:
 * @Author chenzhicai
 * @Date:Created in 15:50 2018/8/16
 * @Version:
 */
@Component
public class CookieUtils {
    //获取cookie
    public static String getCookie(HttpServletRequest request, String cookieName){
        Cookie[] cookies =  request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(cookieName)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
    //设置cookie
    public static void writeCookie(HttpServletResponse response, String cookieName,String                       
        value){
        Cookie cookie = new Cookie(cookieName,value);
        cookie.setPath("/");
        cookie.setMaxAge(3600);
        response.addCookie(cookie);
    }
    
    //移除cookie
    public static void outCookie(HttpServletResponse response, String cookieName,String             
        value){
        Cookie cookie = new Cookie(cookieName,value);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }



}


 

你可能感兴趣的:(deom,常用)