• 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);
        }
    }
  • 相关阅读:
    Fastjson1.2.68 绕Autotype的一点总结
    MySQL外键约束冲突异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (...)
    SO_REUSEADDR
    Live2d Test Env
    Mac上使用virtualenv配置Python虚拟环境
    Mac环境下替换pip源方法
    Mac上安装Python3教程
    【ASP.NET Core学习】使用JWT认证授权
    LeetCode 110. 平衡二叉树 | Python
    LeetCode 733. 图像渲染 | Python
  • 原文地址:https://www.cnblogs.com/gisblogs/p/5038331.html
Copyright © 2020-2023  润新知