Cookie 是什么?
简单的说就是服务器保存在浏览器上的数据。浏览器每次请求的时候,会自动携带这些数据。
Cookie 能做什么?
网上商城的购物车、保持用户登录状态以及单点登录等等
Cookie 怎么玩?
Cookie cookie = new Cookie("age", "18"); //创建cookie,只能放字符串
cookie.setHttpOnly(true); //是否只读,默认false,设置为true后,客户端脚本语言不能修改,如js
cookie.setSecure(false); //是否只能在ssl下使用,默认false
cookie.setMaxAge(-1); //过期时间,单位秒。0表示立即过期,-1表示浏览器关闭后过期,默认-1,过期就是删除该条cookie,如果>0,cookie将会保存在磁盘上
//作用路径,告诉浏览器哪些请求路径才可以携带cookie,/表示根路径,以http://127.0.0.1:8080/...请求的路径都会携带cookie
//如果cookie.setPath("/web/"); 表示以/web/...请求的路径才会携带cookie
//如果不设置,默认以当前请求路径下有效, 如http://127.0.0.1:8080/web/admin/index.jsp 则path=/web/admin/
cookie.setPath("/");
//作用域,默认为localhost,所以同一台服务器上不同的应用是可以共享cookie的,和端口号无关
//如果想多台服务器共享cookie,这里只要设置为顶级域名就可以了
//例如,请求http://web01.fg.cn/ http://web02.fg.cn/这两个地址时,就会携带cookie,主要用于单点登录
cookie.setDomain(".fg.cn"); //最好加上前面这个.,为什么?百度百度一下
response.addCookie(cookie); //向浏览器发送cookie
Cookie[] cookies = request.getCookies(); //从request中获取所有cookie
for (Cookie c : cookies) {
String cName = c.getName();//获取cookie名
String cValue = c.getValue();//获取cookie值
}
Cookie 工具类
public class CookieUtil {
public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
Cookie[] cookies = request.getCookies();
if (cookies == null || cookieName == null) {
return null;
}
String retValue = null;
try {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(cookieName)) {
if (isDecoder) {// 如果涉及中文
retValue = URLDecoder.decode(cookies[i].getValue(), "UTF-8");
} else {
retValue = cookies[i].getValue();
}
break;
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return retValue;
}
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
String cookieValue, int cookieMaxage, boolean isEncode) {
try {
if (cookieValue == null) {
cookieValue = "";
} else if (isEncode) {
cookieValue = URLEncoder.encode(cookieValue, "utf-8");
}
Cookie cookie = new Cookie(cookieName, cookieValue);
if (cookieMaxage >= 0)
cookie.setMaxAge(cookieMaxage);
if (null != request)// 设置域名的cookie
// 访问的域名
cookie.setDomain(getDomainName(request));
/**
* WebContent index.jsp cookie.setDomain("localhost"); // 设置cookie的作用域
* localhsot:8080/webTest/index.jsp 成功! 127.0.0.1:8080/webTest/index.jsp 访问失败!
*
*/
// 当前根目录
cookie.setPath("/");
/**
* WebContent index.jsp index/index1.jsp cookie.setPath("/index");
*/
response.addCookie(cookie);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到cookie的域名
*/
private static final String getDomainName(HttpServletRequest request) {
String domainName = null;
String serverName = request.getRequestURL().toString();
if (serverName == null || serverName.equals("")) {
domainName = "";
} else {
serverName = serverName.toLowerCase();
serverName = serverName.substring(7);
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];
}
System.out.println("domainName = " + domainName);
return domainName;
}
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
setCookie(request, response, cookieName, null, 0, false);
}
}