1.会话跟踪技术
(1)一个浏览器打开到一次浏览器关闭成为一次会话。
(2)会话跟踪:一种维护浏览器状态的方法。http协议是无状态的,
每次发起请求是都会视为新的请求,但服务器需要识别多个请求是否来自同一个服务器,
以便在同一次会话中共享数据,这时就需要会话跟踪技术。
(3)实现方式:session和cookie
2.cookie
2.1 cookie的基本使用
(1)cookie:客户端会话技术,将数据保存到客户端,以后每一次请求都携带cookie进行访问。
(2)cookie的基本使用:浏览器访问服务器后服务器会创建一个cookie发送给客户端,之后客户端
每次请求都会携带所有cookie进行访问,服务器可以获取所有cookie并查找有没有自己想要的cookie。
下面用AServlet向浏览器发送cookie,BServlet获取客户端下一次会话的cookie
@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("username","zs");
response.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if("username".equals(name)){
String value = cookie.getValue();
System.out.println(name+":"+value);
break;
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {
this.doGet(request, response);
}
}
2.2 cookie的使用原理
(1)服务器向客户端发送cookie时会在响应头加上set-cookie发送cookie
(2)客户端携带cookie向服务器发送送请求时会在请求头加上cookie携带所有cookie
2.3 cookie的存活时间
在doGet()方法中的代码如下:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("username","zs");
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
}
2.4 cookie存储中文
下面是向客户端发送cooki将中文进行编码和向服务器发起请求服务器解析中文编码的代码
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String value = "张三";
value = URLEncoder.encode(value, "UTF-8");
System.out.println("存储数据:"+value);
Cookie cookie = new Cookie("username",value);
cookie.setMaxAge(60*60*24*7);
response.addCookie(cookie);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
if("username".equals(name)){
String value = cookie.getValue();
value = URLDecoder.decode(value,"UTF-8");
System.out.println(name+":"+value);
break;
}
}
}
3.session
3.1 session的基本使用
session是服务端的技术,将数据保存到服务端。
相比于cookie来说,session会占用服务端资源,但会更加安全。
下面是利用session存储数据和获取数据的代码
@WebServlet("/demo1")
public class SessionDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("username","zs");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
@WebServlet("/demo2")
public class SessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Object username = session.getAttribute("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
3.2 session的使用原理
(1)同一次会话的多个请求得到的是同一个session,不同会话访问的不是同一个session
(2)session是基于cookie实现的,当会话第一次获取session是,服务端会生成一个sessionid
存入cookie并发送给前段,当会话再一次发起请求时,请求会携带cookie,服务器会根据cookie
中的sessionid的值找到对应服务端相应的session。
3.3 session的钝化、活化和销毁
session对象调用invalidat()方法进行销毁的代码如下:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
System.out.println(session);
session.invalidate();
Object username = session.getAttribute("username");
System.out.println(username);
}
4.cookie和session的区别
5.注意事项
当浏览器关闭时,cookie如果没有设定时销毁的话是会自动被销毁的,此时cookie的sessionid也将不会存在。
所以当重启浏览器时(开启了一个新会话),此时请求访问服务器时得到的session对象已经不是关闭前的那一
个session对象了。
|