一、会话管理概述
1、什么是会话? 好比一次通话。打开浏览器,点击多次链接(发出多次请求和收到多次的响应),关闭浏览器,这个过程就是一次会话。 2、解决的问题是什么? 共享多次请求中产生的数据。比如购物车设计等
二、客户端技术:Cookie
0、Http协议的消息头
请求消息头:Cookie 客户端向服务器端传递信息
响应消息头:Set-Cookie 服务器端向客户端传递信息
1、cookie详解
属性: name:Cookie的名称,必要的属性 value:Cookie的取值(不能为中文),必要的属性 -------------------以下为可选属性 path:Cookie的路径 默认值就是写cookie的那个资源的访问路径 比如:http://localhost:8080/CookieSession/servlet/CookieDemo1
path就是/CookieSession/servlet/
注意: 如果一个存在浏览器缓存中的cookie的路径是/CookieSession/servlet/ 当访问http://localhost:8080/CookieSession/CookieDemo1时,浏览器根本不带Cookie给服务器。浏览器比对的是cookie的路径和当前访问的资源的路径。
浏览器满足一下条件就会带cookie给服务器: 当前访问的地址的路径.startWith(已存cookie的路径)。
举例理解Cookie的path
public class CookieDemo1 extends HttpServlet { //CookieDemo1的映射路径为 /servlet/CookieDemo1
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("lyh","liyanhai");
//将此cookie的路径设置为当前应用 则访问该应用的任何资源时都会带着
//cookie.setPath(request.getContextPath());
// System.out.println(cookie.getPath());
response.addCookie(cookie);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}public class CookieDemo2 extends HttpServlet { //CookieDemo2 的映射路径为 /servlet/CookieDemo2
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie [] cookies = request.getCookies();
if(cookies != null && cookies.length >0){
for(int i=0; i<cookies.length; i++){
if(cookies[i].getName().equals("lyh")){
System.out.println("cookiePath:" + cookies[i].getPath() + "--------cookieValue:" + cookies[i].getValue());
break;
}
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
public class CookieDemo3 extends HttpServlet { //CookieDemo3 的映射路径为 /CookieDemo3
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("URL:" + request.getRequestURL());
System.out.println("URI:" + request.getRequestURI());
Cookie []cookies = request.getCookies();
if(cookies == null){
response.getWriter().write("no cookie");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
} 当我们访问最开始访问http://localhost:8080/CookieSession/servlet/CookieDemo1时,客户端浏览器是不带任何cookie的,
而此时的CookieDemo1服务端向客户端浏览器写了一个cookie,

,此时由于我们没有给该cookie指定任何的path,所以它默认的path就是 /SessionCookie/servlet,
而在我们访问 http://localhost:8080/CookieSession/servlet/CookieDemo2时,由于浏览器客户端的内存中有了一个 cookie:name=lyh,value=liyanhai
的cookie,所以此时当访问资源的时候会将这个cookie带过去。
即:如果一个Cookie的路径设置为了当前应用,说明访问该网站的任何资源时浏览器都带该cookie给服务器。(开发中经常做的)
maxAge:Cookie的最大生存时间。默认是在浏览器的内存中。 domain:Cookie的域名(网站)。默认就是写cookie的那个资源所属的网站。 http://localhost:8080/CookieSession/servlet/CookieDemo1域名就是localhost
version:版本号 comment:注释
把Cookie写给客户端: HttpServletResponse.addCookie(Cookie c):实际上就是向客户端发送了一个响应消息头。 注意:客户端只能保存一个网站最多20个Cookie数据,总共最多300个。每个Cookie的大小不能超过4kb。 服务器端获取客户端带来的Cookie: Cookie [] HttpServletRequest.getCookies();
注意:domain+path+name唯一确定一个Cookie。
三、服务端技术:Session