Cookie在开发中经常使用,我们知道,它是一个用在客户端的。下面编写CookieUtils工具类,方便开发。
- 编写CookieUtils类
public class CookieUtils {
/**
* 设置cookie
* @param request
* @param response
* @param cookieName cookie名称
* @param cookieValue cookie值
* @param cookieMaxAge cookie有效期 -1(浏览器关闭时)
* @param encoding 编码
*/
private static void doSetCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge,String encoding){
if(cookieValue==null){
cookieValue="";
}
try {
cookieValue=URLEncoder.encode(cookieValue, encoding);
Cookie cookie=new Cookie(cookieName, cookieValue);
if(cookieMaxAge>0){
cookie.setMaxAge(cookieMaxAge);
}
if(request!=null){
String domainName = getDomain(request);
cookie.setDomain(domainName);
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
*
* @param response
* @param cookieName 名称
* @param cookieValue 值
* @param cookieMaxAge 有效期
* @param isEncoder 是否编码,如果编码,则使用utf-8
*/
private static void doSetCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge,boolean isEncoder){
if(cookieValue==null){
cookieValue="";
}
try {
if(isEncoder){
cookieValue=URLEncoder.encode(cookieValue, "utf-8");
}
Cookie cookie=new Cookie(cookieName, cookieValue);
if(cookieMaxAge>0){
cookie.setMaxAge(cookieMaxAge);
}
if(request!=null){
String domainName = getDomain(request);
cookie.setDomain(domainName);
}
cookie.setPath("/");
response.addCookie(cookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* 指定编码格式
* @param response
* @param cookieName
* @param cookieValue
* @param cookieMaxAge
* @param encoding
*/
public static void setCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge,String encoding){
doSetCookie(request,response, cookieName, cookieValue, cookieMaxAge, encoding);
}
/**
* 默认使用utf-8进行url编码
* @param response
* @param cookieName
* @param cookieValue
* @param cookieMaxAge
* @param isEncoder
*/
public static void setCookie(HttpServletRequest request,HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge,boolean isEncoder){
doSetCookie(request,response, cookieName, cookieValue, cookieMaxAge, isEncoder);
}
/**
*
* @param request
* @param cookieName 名称
* @param encoding 编码
* @return
*/
public static String getCookieValue(HttpServletRequest request,String cookieName,String encoding){
Cookie[] cookies = request.getCookies();
Cookie findCookie=null;
for(Cookie c:cookies){
if(cookieName.equals(c.getName())){
findCookie=c;
break;
}
}
if(findCookie!=null){
try {
return URLDecoder.decode(findCookie.getValue(), encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 默认使用utf-8进行解码
* @param request
* @param cookieName
* @param isEncoder 是否解码
* @return
*/
public static String getCookieValue(HttpServletRequest request,String cookieName,boolean isEncoder){
Cookie[] cookies = request.getCookies();
Cookie findCookie=null;
if(cookies!=null){
for(Cookie c:cookies){
if(cookieName.equals(c.getName())){
findCookie=c;
break;
}
}
}
if(findCookie!=null){
try {
if(isEncoder){
return URLDecoder.decode(findCookie.getValue(), "utf-8");
}else{
return findCookie.getValue();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 获取域名
* @param request
* @return
*/
private static String getDomain(HttpServletRequest request){
String domainName="";
String serverName = request.getRequestURL().toString();
if(serverName==null||"".equals(serverName)){
domainName="";
}else{
serverName = serverName.toLowerCase();
serverName=serverName.substring(7);
int end=serverName.indexOf("/");
serverName=serverName.substring(0, end);
String[] array = serverName.split("\\.");
for(int i=1;i<array.length;i++){
domainName+=array[i]+".";
}
if(array.length==1){
domainName=serverName;
}else{
domainName="."+domainName.substring(0, domainName.length()-1);
}
}
if(domainName!=null&&domainName.indexOf(":")>0){
String[] arr = domainName.split(":");
domainName=arr[0];
}
return domainName;
}
}
使用cookie需要注意一下几点:
- cookie.setDomain()方法,主要用在多域名情况下。假设有我们登录域名为:www.kanyoo.cn,下次我们登录域名为:console.kanyoo.cn,如果cookie.setDomain(“.kanyoo”),则在www.kanyoo.cn设置cookie,在console.kanyoo.cn也可以生效。如果不设置默认为当前网站的域名,如www.cooay.com的domain默认是www.cooay.com.
- cookie.setMaxAge()方法,主要是设置cookie的有效期,如果为-1或者不设置,则cookie的生命周期为浏览器关闭的时候,cookie就死亡。如果设置大于0的int值,则cookie会在客户端的浏览器以文件的形式进行保存,并且有效期为设置的int值。如半个小时cookie.setMaxAge(30*60)
- request.getCookies()方法,返回值有可能为null,所以需要进行非空验证。