zoukankan      html  css  js  c++  java
  • 【笔记】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;
       } 
    
    }
    

     笔记

  • 相关阅读:
    classloader加载类原理
    要学的东西
    ArcGIS 10 帮助
    ArcGIS Server发布移动地图服务(mobile data access)时的问题
    SDE的性能取决于
    用oracle trace 来跟踪session
    ArcSDE10升级sp1后服务无法启动的解决办法
    ArcSDE 发布 WMS 服务出错
    ArcSDE10升级sp1后服务无法启动的解决办法
    ArcGIS Server发布移动地图服务(mobile data access)时的问题
  • 原文地址:https://www.cnblogs.com/mybug/p/5563926.html
Copyright © 2011-2022 走看看