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;
public class CookieUtil {
/**
* 增删改Cookie
* @param key
* @param value
* @param maxAge
* @return
* @throws UnsupportedEncodingException
*/
public Cookie getCookie(String key, String value, int maxAge)
throws UnsupportedEncodingException {
value = URLEncoder.encode(value, "GB18030");
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(maxAge);
cookie.setPath("/");
return cookie;
}
/**
* 查询指定cookie
* @param key
* @param cookie
* @return
* @throws UnsupportedEncodingException
*/
public String getCookie(String key, Cookie cookie)
throws UnsupportedEncodingException {
if (key.equals(cookie.getName())) {
return URLDecoder.decode(cookie.getValue(), "GB18030");
}
return null;
}
}
CookieUtil类
最新推荐文章于 2020-06-16 13:41:52 发布