1、cookie小甜饼:
服务器端将文本信息(session可以存对象)存储在客户端。
模型:
:Cookie是以name:value存放的,都是String类型
2、服务器如何将数据写入客户端(cookie)
//新建cookie对象
Cookie ck = new Cookie(“name”,”siggy”);
//通过response对象,将cookie写入客户端
response.addCookie(ck);
3、Cookie不去设置失效时间,默认用完删除。
setMaxAge(int expiry)
设置cookie的失效时间,如果等于零,那么是删除。如果小于零,那么不存储。如果大于零,是设置的秒数。
当不设置的时候,储存在本地磁盘。
当setMaxAge(-1);不存储的指的是不存磁盘,但是在浏览器中存在。当关闭浏览器就没有了。
当等于零的时候,cookie会回写到客户端,但是会被立即删除,下一次请求将不会把该cookie携带到请求中。
当大于零的时候,会存在磁盘上,当浏览器关闭,下一次请求时一样会携带到服务器上。
4、取cookie中的值
//获取cookie值
Cookie[] cks = req.getCookie();
If(cks!=null)
for(Cookie ck:cks){
System.out.println(ck.getName()+”----”+ck.getValue());
}
Cookie的name和value。会随请求传到服务器端。
Cookie的其他信息有浏览器管理。
5、Cookie应用
CookieLogin:获取Cookie值
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
String name = null;
String password = null;
Cookie cks[] = req.getCookies();
if(cks != null)
for(Cookie ck:cks) {
if("name".equals(ck.getName()))
name = ck.getValue();
if("password".equals(ck.getName()))
password = ck.getValue();
}
out.print("<html>");
out.print("<head>");
out.print("</head>");
out.print("<body>");
out.print("<form action='Welcome' method='post'>");
out.print("用户名:<input type='text' name='name' value='"+(name!=null?name:"")+"'>");
out.print("<br/>");
out.print("用户名:<input type='password' name='password' value='"+(password!=null?password:"")+"'>");
out.print("<br/>");
out.print("<input type='checkbox' name='remember' value='1'/>保存用户名和密码两周");
out.print("<input type='submit' value='登录'>");
out.print("</form>");
out.print("</body>");
out.print("</html>");
}
AddCookie:在浏览器中写入Cookie
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
String remember = req.getParameter("remember");
if("1".equals(remember))
{
Cookie ck = new Cookie("name",req.getParameter("name"));
Cookie ck2 = new Cookie("password",req.getParameter("password"));
ck.setMaxAge(2*7*24*3600);
ck.setMaxAge(2*7*24*3600);
resp.addCookie(ck);
resp.addCookie(ck2);
out.print("Cookie写入本地磁盘成功!!!");
} else
{
out.print("Cookie写入本地磁盘失败!!!");
}
}
6、Cookie覆盖
cookie没有修改,如果想修改某个cookie的值,只需要新建一个name相同的cookie,即可将原来的覆盖,cookie也不需要删除,当cookie到期由浏览器来维护。
7、Cookie是带有domain(域名)的,每个web应用只能读取自己的cookie,cookie和浏览器有关,不同浏览器cookie是不能共享的。
8、Cookie是由浏览器管理,cookie是可以被禁用的。
9、Cookie vs session
Cookie可以存放大量的文本数据。
安全性低,效率低
Session数据小,安全性高,效率高(可以存对象)。
存student stu = new student(“张安”,”男”,23);
Session.setAttribute(“stu”,stu);
Cookie ck = new(“student”,”1_张安_男_23”);
10、cookie的中文处理
Cookie写入浏览器,编码:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
Cookie ck = new Cookie(URLEncoder.encode("姓名","utf-8"),URLEncoder.encode("喻超","utf-8"));
resp.addCookie(ck);
resp.sendRedirect("index.jsp");
}
Cookie取出显示,解码:
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Cookie[] cks = request.getCookies();
for(Cookie ck:cks){
out.print(URLDecoder.decode(ck.getName(),"utf-8")+"-----"+URLDecoder.decode(ck.getValue(),"utf-8")+"<br/>");
}
%>
</body>
</html>