zoukankan      html  css  js  c++  java
  • cookieUtil

    public class CookieUtil {
        
        
        /**
         * 设置cookie
         * @param name  cookie名字
         * @param value cookie值
         * @param maxAge cookie生命周期  以秒为单位
         */
        public static void addCookie(HttpServletResponse res,String name,String value,String domain,int maxAge){
            
            
            try {
                value = URLEncoder.encode(""+value, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            
            Cookie cookie = new Cookie(name, value + "" );
            cookie.setPath("/");
            cookie.setDomain(domain);
            if(maxAge>0)  cookie.setMaxAge(maxAge);
            res.addCookie(cookie);
        }
        
        /**
         * 根据名字获取cookie
         * @param name cookie名字
         * @return
         */
        public static Cookie getCookieByName(HttpServletRequest req,String name){
            Map<String,Cookie> cookieMap = getCookieMap(req);
            if(cookieMap.containsKey(name)){
                Cookie cookie = (Cookie)cookieMap.get(name);
                return cookie;
            }else{
                return null;
            }   
        }
        
        /**
         * 将cookie封装到Map里面
         * @return
         */
        private static Map<String,Cookie> getCookieMap(HttpServletRequest req){  
            Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
            Cookie[] cookies = req.getCookies();
            if(null!=cookies){
                for(Cookie cookie : cookies){
                    cookieMap.put(cookie.getName(), cookie);
                }
            }
            return cookieMap;
        }
    
        
        public static boolean deleteCookie(HttpServletRequest req,HttpServletResponse res,String cookieName) {   
            if (cookieName != null) {
                Cookie cookie = getCookie(req,cookieName);   
                if(cookie!=null){
                    cookie.setMaxAge(0);//0,就立即删除   
                    cookie.setPath("/");//不要漏掉   
                    cookie.setDomain(req.getServerName());
                    res.addCookie(cookie);   
                    return true;   
                }
            }   
            return false;   
        }
    
        public static String getValue(HttpServletRequest req,String cookieName){
            Cookie cookie = getCookie(req,cookieName);
            return getValue(cookie);
        }
        
        public static String getValue(Cookie cookie){
            if(null == cookie) return null;
            try {
                return URLDecoder.decode(cookie.getValue(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
        
        public static Cookie getCookie(HttpServletRequest req,String cookieName){   
            Cookie[] cookies = req.getCookies();   
            Cookie cookie = null;   
            try {
                if (cookies != null && cookies.length > 0) {   
                    for (int i = 0; i < cookies.length; i++) {
                        cookie = cookies[i];   
                        if (cookie.getName().equals(cookieName)) {   
                            return cookie;   
                        }   
                    }   
                }   
            } catch (Exception e) {  
                e.printStackTrace();   
            }
            return null;   
        }   
                                        
    }

    关于cookie util

  • 相关阅读:
    如何用js得到当前页面的url信息方法(JS获取当前网址信息)
    可拖动大小div案例
    div内div水平垂直居中
    div设置absolute情况下填充剩余宽度
    最近很不顺
    [转载]什么是对象序列化,为什么要使用
    mac下安装eclipse以及python
    Myeclipse 10 for mac 破解版下载安装及破解方法
    IOS7学习之路一(新UI之自定义UITableViewCell)
    Xcode5和ObjC新特性
  • 原文地址:https://www.cnblogs.com/zhouweidong/p/6857139.html
Copyright © 2011-2022 走看看