cookie的工具类

public class CookieUtil {
    //增加
    public static void addCookie(HttpServletResponse response,String name,String value,int maxAge){
        Cookie cookie=new Cookie(name, value);
        cookie.setPath("/");
        if(maxAge>0){
            cookie.setMaxAge(maxAge);
        }
        response.addCookie(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;
    }
    //删除
    public static void removeCookie(HttpServletResponse response,String name){
       Cookie cookie=new Cookie(name,null);
       cookie.setPath("/");
       cookie.setMaxAge(0);
       response.addCookie(cookie);


    }


}

你可能感兴趣的:(JAVA工具类,Cookie)