자바 쿠키 의 첨삭 검사

10373 단어 도구 클래스
package com.test.cookie;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * Cookie        crud
 *   :
 *   Cookie2   :
 *   1,  foreach       value;
 *   2  map   
 */
public class CookieUtils {
    //   ,     
    //        ,  // java.lang.IllegalArgumentException: Control character in cookie value or attribute
    public static void addCookie(HttpServletResponse response, String name,
            String value, int maxAge) {
        //       ,  ,      
        String utfvalue = null;
        try {
            utfvalue = URLEncoder.encode(value.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Cookie cookie = new Cookie(name.trim(), utfvalue);

        //     ,  Cookie              ,               !
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);//    
        }
        //     ,               cookie;       ,       cookie           
        cookie.setPath("/");
        //   
        response.addCookie(cookie);
//      System.out.println("    !");
    }

    //  
    //  2   ,     foreach       value, 2   map   
    //      ,  name  cookie  ,    name
    public static Cookie getCookie(HttpServletRequest request, String name) {
    Map map = getCookieMap(request, name);
        if (map.containsKey(name)) {
            return map.get(name);
        }
        return null;
    }

private static Map getCookieMap(HttpServletRequest request,
            String name) {
        Cookie coos[] = request.getCookies();
        Map cookieMap = new HashMap<>();
        if (coos != null) {
            for (Cookie cookie : coos) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    //      ,    
    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie coos[] = request.getCookies();
        if (coos != null) {
            for (Cookie cookie : coos) {
                if (cookie.getName().equals(name)) {
                    try {
                //          ,       
                        return URLDecoder.decode(cookie.getValue(), "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
        return null;
    }

    //  ,      value 
    //   、  Cookie ,   Cookie value、maxAge       ,  name、path、domain ,    Cookie    。
    //   ,           Cookie    ,    、    。
    //      :1,        cookie.getName().equals(name),      
    //            2,        addCookie    
    //      1
    //     Cookie  1 (  , Cookie         )
    public static boolean updateCookie(HttpServletRequest request,HttpServletResponse response, String name, String value) {
        return updateCookie(request, response, name, value, 1*24*60*60);
    }
    //  Cookie  
    public static boolean updateCookie(HttpServletRequest request,
            HttpServletResponse response, String name, String value,int maxAge) {
        String utfvalue=null;
        try {
            //    
            utfvalue=URLEncoder.encode(value.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Cookie coos[] = request.getCookies();
        boolean flag = false;
        if (coos != null) {
            for (Cookie cookie : coos) {
                if (cookie.getName().equals(name)) {
                    try {
System.out.println("   :" + URLDecoder.decode(cookie.getValue(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    cookie.setValue(utfvalue);
                    cookie.setPath("/");
                    cookie.setMaxAge(maxAge);
                    response.addCookie(cookie);
                    flag = true;
                    break;
                }
            }
            if (flag==false) {
                System.out.println("         Cookie!");
            }
        }
        return flag;
    }


    //  
    //   1:      ,      
    public static boolean deleteCookie(HttpServletRequest request,
            HttpServletResponse response, String name) {
        Cookie coos[] = request.getCookies();
        boolean flag = false;
        if (coos != null) {
            for (Cookie cookie : coos) {
                if (cookie.getName().equals(name)) {
                    //cookie.setValue(null);
                    //       
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    System.out.println("    cookie   :"+cookie.getName());
                    flag=true;
                    break;
                }
            }
        }
        return flag;
    }

    //  2:   ,    ,    ,     
           public static void deleteCookie1( HttpServletResponse response,String name){
                  Cookie cookie = new Cookie(name, null);
                  cookie.setMaxAge(0);
                  cookie.setPath("/");
                  response.addCookie(cookie);
           }
}

좋은 웹페이지 즐겨찾기