【笔记】cookies管理工具类

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;
   } 

}

 笔记

posted @ 2016-06-06 15:20  我的bug  阅读(267)  评论(0编辑  收藏  举报