• Java_CookieUtil


    package com.willow.util;
     
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    /**
     * CookieUtil用来操作cookie的存取
     * @author GetInstance
     *
     */
    public class CookieUtil {
        /**
         * 添加cookie
         * @param name cookie的key
         * @param value cookie的value
         * @param domain domain
         * @param  path path 
         * @param maxage  最长存活时间 单位为秒
         * @param response
         */
        public static void addCookie(String name ,String value,String domain,
                int maxage,String path, HttpServletResponse response){
            Cookie cookie = new Cookie(name,value);
            if(domain!=null){
                cookie.setDomain(domain);
            }
            cookie.setMaxAge(maxage);
            cookie.setPath(path);
            response.addCookie(cookie);
        }
         
        /**
         * 往根下面存一个cookie
         * * @param name cookie的key
         * @param value cookie的value
         * @param domain domain
         * @param maxage  最长存活时间 单位为秒
         * @param response
         */
        public static void addCookie(String name ,String value,String domain,
                int maxage, HttpServletResponse response){
            addCookie(name, value,domain, maxage, "/" , response);
        }
         
        /**
         * 从cookie值返回cookie值,如果没有返回 null
         * @param req
         * @param name
         * @return cookie的值
         */
        public static String getCookie(HttpServletRequest request, String name) {
            Cookie[] cookies = request.getCookies();
            if (cookies == null) return null;
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(name)) {
                    return cookies[i].getValue();
                }
            }
            return null;
        }
     
        public static void removeCookie(String name, String domain, HttpServletRequest request, HttpServletResponse response) {
            String cookieVal = getCookie(request,name);
            if(cookieVal!=null){
                CookieUtil.addCookie(name, null, domain, 0, response);
            }
        }
     
        public static void removeCookie(String name, HttpServletRequest request, HttpServletResponse response) {
            CookieUtil.removeCookie(name, ".dhgate.com", request, response);
        }
    }
  • 相关阅读:
    2.15 STL复习
    20190214Test(栈与队列)
    STL列表链式前向星
    链式前向星(邻接表)
    Priority_queue详解
    List详解
    NOIP2019计划
    第二章笔记
    第一章笔记
    本地文件上传GitHub
  • 原文地址:https://www.cnblogs.com/gisblogs/p/5038331.html
Copyright © 2020-2023  润新知