packagecom.DingYu.Cookie;importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.net.URLEncoder;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/***
* Cookie 工具类
**/
public final classCookieUtils {/*** 得到Cookie的值, 不编码
*
*@paramrequest
*@paramcookieName
*@return
*/
public staticString getCookieValue(HttpServletRequest request, String cookieName) {return getCookieValue(request, cookieName, false);
}/*** 得到Cookie的值,
*
*@paramrequest
*@paramcookieName
*@return
*/
public static String getCookieValue(HttpServletRequest request, String cookieName, booleanisDecoder) {//获得所有的Cookie
Cookie[] cookieList =request.getCookies();if (cookieList == null || cookieName == null) {return null;
}
String retValue= null;try{for (int i = 0; i < cookieList.length; i++) {if(cookieList[i].getName().equals(cookieName)) {//如果true 使用UTF-8 进行解码
if(isDecoder) {
retValue= URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
}else{
retValue=cookieList[i].getValue();
}break;
}
}
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}returnretValue;
}/*** 得到Cookie的值,使用自定义的编码类型进行解码
*
*@paramrequest
*@paramcookieName
*@return
*/
public staticString getCookieValue(HttpServletRequest request, String cookieName, String encodeString) {
Cookie[] cookieList=request.getCookies();if (cookieList == null || cookieName == null) {return null;
}
String retValue= null;try{for (int i = 0; i < cookieList.length; i++) {if(cookieList[i].getName().equals(cookieName)) {//使用自定义的编码类型进行解码
retValue =URLDecoder.decode(cookieList[i].getValue(), encodeString);break;
}
}
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}returnretValue;
}/*** 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码*/
public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue) {
setCookie(request, response, cookieName, cookieValue,-1);
}/*** 设置Cookie的值 在指定时间内生效,但不编码*/
public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue,intcookieMaxage) {
setCookie(request, response, cookieName, cookieValue, cookieMaxage,false);
}/*** 设置Cookie的值 不设置生效时间,但编码*/
public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue,booleanisEncode) {
setCookie(request, response, cookieName, cookieValue,-1, isEncode);
}/*** 设置Cookie的值 在指定时间内生效, 编码参数*/
public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue,int cookieMaxage, booleanisEncode) {
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode);
}/*** 设置Cookie的值 在指定时间内生效, 编码参数(指定编码)*/
public static voidsetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue,intcookieMaxage, String encodeString) {
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString);
}/*** 删除Cookie带cookie域名,删除原理,把原来的cookie的Value用""*/
public static voiddeleteCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName) {
doSetCookie(request, response, cookieName,"", -1, false);
}/*** 设置Cookie的值,并使其在指定时间内生效
*
*@paramcookieMaxage cookie生效的最大秒数*/
private static final voiddoSetCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue,int cookieMaxage, booleanisEncode) {try{if (cookieValue == null) {
cookieValue= "";
}else if(isEncode) {//使用UTF-8进行编码
cookieValue = URLEncoder.encode(cookieValue, "utf-8");
}
Cookie cookie= newCookie(cookieName, cookieValue);if (cookieMaxage > 0)//设置Cookie的生存时间
cookie.setMaxAge(cookieMaxage);if (null != request) {//设置域名的cookie
String domainName =getDomainName(request);
System.out.println(domainName);if (!"localhost".equals(domainName)) {
cookie.setDomain(domainName);
}
}
cookie.setPath("/");
response.addCookie(cookie);
}catch(Exception e) {
e.printStackTrace();
}
}/*** 设置Cookie的值,并使其在指定时间内生效
*
*@paramcookieMaxage cookie生效的最大秒数*/
private static final voiddoSetCookie(HttpServletRequest request, HttpServletResponse response,
String cookieName, String cookieValue,intcookieMaxage, String encodeString) {try{if (cookieValue == null) {
cookieValue= "";
}else{
cookieValue=URLEncoder.encode(cookieValue, encodeString);
}
Cookie cookie= newCookie(cookieName, cookieValue);if (cookieMaxage > 0)
cookie.setMaxAge(cookieMaxage);if (null != request) {//设置域名的cookie
String domainName =getDomainName(request);if (!"localhost".equals(domainName)) {
cookie.setDomain(domainName);
}
}
cookie.setPath("/");
response.addCookie(cookie);
}catch(Exception e) {
e.printStackTrace();
}
}/*** 得到cookie的域名*/
private static finalString getDomainName(HttpServletRequest request) {
String domainName= null;//获得请求的域名
String serverName =request.getRequestURL().toString();if (serverName == null || serverName.equals("")) {
domainName= "";
}else{//全转为小写
serverName =serverName.toLowerCase();//从第七个开始截取,过滤掉http:// serverName = serverName.substring(7);//到后面的参数为止 比如 www.taobao.com/name=xxx
final int end = serverName.indexOf("/");//把参数截取
serverName = serverName.substring(0, end);//根据点来截取
final String[] domains = serverName.split("\\.");int len =domains.length;if (len > 3) {//www.xxx.com.cn
domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
}else if (len <= 3 && len > 1) {//xxx.com or xxx.cn
domainName = "." + domains[len - 2] + "." + domains[len - 1];
}else{
domainName=serverName;
}
}//去掉端口号
if (domainName != null && domainName.indexOf(":") > 0) {
String[] ary= domainName.split("\\:");
domainName= ary[0];
}returndomainName;
}
}