package util;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

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

public class CookieBox {

    private Map<String, Cookie> cookieMap = new java.util.HashMap<String, Cookie>();
   
    public CookieBox(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
            if ( cookies != null ) {
                for ( int i = 0; cookies.length > i; i++ ) {
                    cookieMap.put(cookies[i].getName(), cookies[i]);
                }
            }
    }
   
    public static Cookie createCookie(String name, String value) throws IOException {
        return new Cookie(name, URLEncoder.encode(value, "euc-kr") );
    }
   
    public static Cookie createCookie(String name, String value, String path, int maxAge) throws IOException {
       
        Cookie cookie = new Cookie(name, URLEncoder.encode(value, "euc-kr") );
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        return cookie;
    }
   
    public static Cookie createCookie(String name, String value, String domain, String path, int maxAge) throws IOException {
       
        Cookie cookie= new Cookie(name, URLEncoder.encode(value, "euc-kr") );
        cookie.setDomain(domain);
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        return cookie;
    }
   
    public Cookie getCookie(String name) {
       
        return cookieMap.get(name);
    }
   
    public String getValue(String name) throws IOException {
       
        return URLDecoder.decode(cookieMap.get(name).getValue(), "euc-kr");
    }
   
    public boolean exists(String name) {
       
        return cookieMap.get(name) != null;
    }
}

'공부 > JAVA_UTILL_CLASS' 카테고리의 다른 글

UTILL_[파일업로드]  (0) 2012.11.19

+ Recent posts