笔记cookies管理工具类

Posted 我的bug

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了笔记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;
   } 

}

 笔记

以上是关于笔记cookies管理工具类的主要内容,如果未能解决你的问题,请参考以下文章

Cookie工具类代码

elasticsearch代码片段,及工具类SearchEsUtil.java

TodoJava学习笔记 100==100 & Reflection API & Optional类详解 & DIPIoCDI & token/cookie/s

Java Cookie工具类

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

C#-WebForm-★内置对象简介★Request-获取请求对象Response相应请求对象Session全局变量(私有)Cookie全局变量(私有)Application全局公共变量Vi(代码片段