博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【笔记】cookies管理工具类
阅读量:6092 次
发布时间:2019-06-20

本文共 4423 字,大约阅读时间需要 14 分钟。

package com.ulearning.ulms.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.HttpServletResponse;import com.ulearning.ulms.core.utils.Constants;public class CookieUtil {	/*	 * 从给定的request中查找cookie	 */    public static String getCookie(HttpServletRequest request,String cookieName){    	String rt=null;    	Cookie[] cookies = request.getCookies();    	if(cookies!=null)    	{    	    for (int i = 0; i < cookies.length; i++)     	    {    	       Cookie c = cookies[i];         	       if(c.getName().equalsIgnoreCase(cookieName))    	       {    	    	   rt= c.getValue();    	    	   break;    	       }    	            	    }     	  }    	try {    		if(rt !=null){    			rt = URLDecoder.decode(rt,"utf-8");    		}else{    			rt = (String)request.getAttribute(cookieName);    		}		} catch (UnsupportedEncodingException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}    	return rt;    }    /**     * 删除,某一个cookie     * @param request     * @param cookieName     * @return     */        public static void clearCookie(HttpServletRequest request,HttpServletResponse response,String cookieName){    	Cookie[] cookies = request.getCookies();    	if(cookies!=null)    	{    	    for (int i = 0; i < cookies.length; i++)     	    {    	       Cookie c = cookies[i];       	              	       if(!c.getName().equalsIgnoreCase(cookieName))    	       {	    	       c.setValue(null);	    	       c.setMaxAge(0);	    	       c.setPath("/");	    	       response.addCookie(c);	    	    	   break;    	       } 	  	           	    }     	 }    }                                public static int getCookieInt(HttpServletRequest request,String cookieName){    	int res = 0;    	String rt=null;    	Cookie[] cookies = request.getCookies();    	if(cookies!=null)    	{    	    for (int i = 0; i < cookies.length; i++)     	    {    	       Cookie c = cookies[i];         	       if(c.getName().equalsIgnoreCase(cookieName))    	       {    	    	   rt= c.getValue();    	    	   break;    	       }    	            	    }     	  }    	try {    		if(rt !=null)    		{    			rt = URLDecoder.decode(rt,"utf-8");    			res = Integer.parseInt(rt);    		}else{    			rt = (String)request.getAttribute(cookieName);    			res = rt!=null ? Integer.parseInt(rt) : 0;    		}		} catch (UnsupportedEncodingException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}    	return res;    }            public static void clearCookie(HttpServletRequest request,HttpServletResponse response){    	String rt=null;    	Cookie[] cookies = request.getCookies();    	if(cookies!=null)    	{    	    for (int i = 0; i < cookies.length; i++)     	    {    	       Cookie c = cookies[i];         	       if(!c.getName().equalsIgnoreCase(Constants.SHOPPING_CART_KEY))    	       {	    	       c.setValue(null);	    	       c.setMaxAge(0);	    	       c.setPath("/");	    	       response.addCookie(c);	    	       /*rt= c.getValue();	    	    	   break;*/    	       } 	            	    }     	}    }        //往cookie里面 写入值 name 是键  value 是 值    public static void addCookie(HttpServletResponse response, String name,            String value, int maxAge) {    	try {			value=URLEncoder.encode(value,"UTF-8");		} catch (UnsupportedEncodingException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}      	        Cookie cookie = new Cookie(name, value);        if (maxAge > 0)            cookie.setMaxAge(maxAge);        cookie.setPath("/");       	cookie.setComment("EXPIRING COOKIE at "+ System.currentTimeMillis());        response.addCookie(cookie);    }        /**     * 方法描述:只从cookie中获取。如果不存在该cookie,返回null。     * @param request     * @param cookieName     * @return     * @author: Huyihui     * @version: 2012-9-25 上午11:03:04     */   public static String getCookieOnly(HttpServletRequest request, String cookieName) {   	Cookie[] cookieArr = request.getCookies();   	if (cookieArr != null && cookieArr.length > 0) {   		for (Cookie cookie : cookieArr) {   			if (cookie.getName().equals(cookieName)) {   				try {						return URLDecoder.decode(cookie.getValue(), "UTF-8");					} catch (UnsupportedEncodingException e) {						e.printStackTrace();					}   			}   		}   	}   	return null;   } }

 笔记

转载于:https://www.cnblogs.com/mybug/p/5563926.html

你可能感兴趣的文章
配置PL/SQL Developer连接Oracle数据库
查看>>
创建CancellationTokenSource对象用于取消Task
查看>>
vue入门实例
查看>>
管理者的角色修炼-第三课-赢在执行
查看>>
Git2
查看>>
禁止windows2003 关机选项
查看>>
Log4Net
查看>>
人生不相见,动如参与商
查看>>
禁止双击选择页面内容
查看>>
HDU5037 Frog
查看>>
程序集冲突问题
查看>>
LeetCode 766. Toeplitz Matrix
查看>>
Java序列化反序列化对象流ObjectInputStream、ObjectOutputStream
查看>>
Spring与Mybatis的整合
查看>>
WinForm 弹框确认后执行
查看>>
Linux面试题
查看>>
! [rejected] master -> master (non-fast-forward)
查看>>
STL unique
查看>>
装饰自己的博客园界面
查看>>
django-返回客户端外网ip服务
查看>>