-
public static void addCookie(HttpServletResponse response,String domain,String name,String value,int maxAge){
-
try {
-
//解决中文报Control character in cookie value, consider BASE64 encoding your value错。
-
value=URLEncoder.encode(value,"UTF-8");
-
} catch (UnsupportedEncodingException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
Cookie cookie = new Cookie(name,value);
-
cookie.setPath("/");
-
if(!domain.equals("")&&domain!=null){
-
cookie.setDomain(domain);
-
}else {
-
cookie.setDomain("."+SystemConfig.getInstance().getDnsName());
-
}
-
if(maxAge>0) cookie.setMaxAge(maxAge);
-
response.addCookie(cookie);
-
}
-
public static String getCookieValue(String cook, HttpServletRequest request) {
-
Cookie[] cookies = request.getCookies();
-
try {
-
for (Cookie one : cookies) {
-
if (one.getName() != null && one.getName().equals(cook))
-
return URLDecoder.decode(one.getValue(),"UTF-8");
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return null;
-
}
-
public static void clearCookiebyName(String cook, HttpServletRequest request,HttpServletResponse response){
-
Cookie[] cookies = request.getCookies();
-
if (cookies != null) {
-
for (Cookie one : cookies) {
-
try {
-
if (one.getName() != null && one.getName().equals(cook)){
-
one.setMaxAge(0);
-
one.setDomain("." + SystemConfig.getInstance().getDnsName());
-
response.addCookie(one);
-
}
-
} catch (Exception e) {
-
-
BitTools.debugPrintln("清楚Cookies发生异常!");
-
}
-
}
-
}
-
-
}
-
public static void clearAllCookie(HttpServletRequest request,HttpServletResponse response){
-
Cookie[] cookies = request.getCookies();
-
Cookie cookie = null;
-
if (cookies != null) {
-
for (int i = 0; i < cookies.length; i++) {
-
try {
-
cookie = cookies[i];
-
cookie.setMaxAge(0);
-
cookies[i].setDomain("." + SystemConfig.getInstance().getDnsName());
-
response.addCookie(cookie);
-
} catch (Exception e) {
-
-
BitTools.debugPrintln("清空Cookies发生异常!");
-
}
-
}
-
}
-
}
阅读(1216) | 评论(0) | 转发(0) |