180425、cookie工具类

package com.thinkgem.jeesite.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

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

/**
 * Cookie工具类
 * @author Rick
 * @version 2018-04-26
 */
public class CookieUtils {

    /**
     * 设置 Cookie(生成时间为1天)
     * @param name 名称
     * @param value 值
     */
    public static void setCookie(HttpServletResponse response, String name, String value) {
        setCookie(response, name, value, 60*60*24);
    }

    /**
     * 设置 Cookie
     * @param name 名称
     * @param value 值
     * @param maxAge 生存时间(单位秒)
     * @param uri 路径
     */
    public static void setCookie(HttpServletResponse response, String name, String value, String path) {
        setCookie(response, name, value, path, 60*60*24);
    }

    /**
     * 设置 Cookie
     * @param name 名称
     * @param value 值
     * @param maxAge 生存时间(单位秒)
     * @param uri 路径
     */
    public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
        setCookie(response, name, value, "/", maxAge);
    }

    /**
     * 设置 Cookie
     * @param name 名称
     * @param value 值
     * @param maxAge 生存时间(单位秒)
     * @param uri 路径
     */
    public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
        Cookie cookie = new Cookie(name, null);
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        try {
            cookie.setValue(URLEncoder.encode(value, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.addCookie(cookie);
    }

    /**
     * 获得指定Cookie的值
     * @param name 名称
     * @return 值
     */
    public static String getCookie(HttpServletRequest request, String name) {
        return getCookie(request, null, name, false);
    }
    /**
     * 获得指定Cookie的值,并删除。
     * @param name 名称
     * @return 值
     */
    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        return getCookie(request, response, name, true);
    }
    /**
     * 获得指定Cookie的值
     * @param request 请求对象
     * @param response 响应对象
     * @param name 名字
     * @param isRemove 是否移除
     * @return 值
     */
    public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, boolean isRemove) {
        String value = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    try {
                        value = URLDecoder.decode(cookie.getValue(), "utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    if (isRemove) {
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                    }
                }
            }
        }
        return value;
    }
}

原文地址:https://www.cnblogs.com/zrbfree/p/8955465.html

时间: 2024-10-29 19:08:25

180425、cookie工具类的相关文章

Cookie工具类

1 import javax.servlet.http.Cookie; 2 import javax.servlet.http.HttpServletRequest; 3 import javax.servlet.http.HttpServletResponse; 4 import java.io.UnsupportedEncodingException; 5 import java.net.URLDecoder; 6 import java.net.URLEncoder; 7 8 /** 9

Cookie工具类 - CookieUtil.java

Cookie工具类,提供Cookie的创建.获取.删除方法. 源码如下:(点击下载 -CookieUtil.java ) import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Cookie工具类 * */ public class CookieUtil { private CookieU

java CookieUtils cookie工具类

1 package com.mohecun.common.utils; 2 3 import java.io.UnsupportedEncodingException; 4 import java.net.URLDecoder; 5 import java.net.URLEncoder; 6 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.serv

Cookie工具类(获取cookie,设置cookie)

import java.io.UnsupportedEncodingException; import java.net.URLDecoder;import java.net.URLEncoder; import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;i

C# Cookie工具类

/// <summary> /// Cookies赋值 /// </summary> /// <param name="strName">主键</param> /// <param name="strValue">键值</param> /// <param name="strDay">有效天数</param> /// <returns>&l

自定义的操作Cookie的工具类

可以在SpringMVC等环境中使用的操作Cookie的工具类 package utils; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.h

java-cookie工具类

package com.gordon.utils; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Cookie工具类 * */ public class CookieUtil { /** * 添加cookie * * @param response * @param name *

CookieUtils工具类

package com.taotao.common.util; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServlet

整理收集的一些常用java工具类

1.json转换工具 1. package com.taotao.utils; 3. import java.util.List; 5. import com.fasterxml.jackson.core.JsonProcessingException; 6. import com.fasterxml.jackson.databind.JavaType; 7. import com.fasterxml.jackson.databind.JsonNode; 8. import com.faster