• 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
    	 * @param value
    	 * @param maxAge
    	 */
    	public static void addCookie(HttpServletResponse response, String name, String value) {
    		Cookie cookie = new Cookie(name, value);
    		cookie.setPath("/");
    		response.addCookie(cookie);
    	}
    
    	/**
    	 * 添加cookie
    	 * 
    	 * @param response
    	 * @param name
    	 * @param value
    	 * @param maxAge
    	 */
    	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);
    	}
    
    	/**
    	 * 删除cookie
    	 * 
    	 * @param response
    	 * @param name
    	 */
    	public static void removeCookie(HttpServletResponse response, String name) {
    		Cookie uid = new Cookie(name, null);
    		uid.setPath("/");
    		uid.setMaxAge(0);
    		response.addCookie(uid);
    	}
    
    	/**
    	 * 获取cookie值
    	 * 
    	 * @param request
    	 * @return
    	 */
    	public static String getUid(HttpServletRequest request, String cookieName) {
    		Cookie cookies[] = request.getCookies();
    		for (Cookie cookie : cookies) {
    			if (cookie.getName().equals(cookieName)) {
    				return cookie.getValue();
    			}
    		}
    		return null;
    	}
    }
    
  • 相关阅读:
    SDN第二次上机作业
    SDN第二次作业
    SDN第一次上机作业
    个人作业-软件产品案例分析
    pandas之Series
    pandas简答介绍
    python爬取英文名
    python爬取动态网页
    python画树
    Python存储数据的方式
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7366523.html
Copyright © 2020-2023  润新知