会话技术
1.会话:一次会话中包含多次请求和响应
一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到一方断开为止
2.功能:在一次会话的范围内的多次请求时间,数据共享
3.方式:
1.客户端会话技术:Cookie
2.服务器端会话技术:Session
Cookie:
1. 概念:客户端会话技术,将数据保存到客户端
2. 快速入门:
使用步骤:
1. 创建Cookie对象,绑定数据
new Cookie(String name, String value)
2. 发送Cookie对象
response.addCookie(Cookie cookie)
3. 获取Cookie,拿到数据
Cookie[ ] request.getCookies()
/* Cookie快速入门 */ @WebServlet("/cookieDemo01") public class CookieDemo01 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.创建Cookie对象 Cookie cook = new Cookie("zs", "Hello"); //2.发送Cookie response.addCookie(cook); } }
@WebServlet("/cookieDemo02") public class CookieDemo02 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取Cookie Cookie[] kies = request.getCookies(); //获取数据,遍历Cookie if (kies!= null) { for (Cookie cook : kies) { String name = cook.getName(); String value = cook.getValue(); System.out.println(name+":"+value); } } } }
标签:Override,request,会话,cook,Cookie,739,738,response From: https://www.cnblogs.com/agzq/p/17110150.html