会话技术_概述
会话:一次会话中包含多次请求和响应
一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止
功能:在一次会话的范围内的多次请求间,共享数据
方式:
1.客户端会话技术:Cookie
2.服务端会话技术:Seesion
会话技术_Cookie快速入门
概念:客户端会话技术,将数据保存到客户端
快速入门:
使用步骤:
1.创建Cookie对象,绑定数据
new Cookie(String name,String value);
2.发送Cookie对象
response.addCookie(Cookie cookie);
3.获取Cookie,拿到数据
Cookie[] request.getCookies();
创建和发送Cookie:
@WebServlet(name = "CookieDemo1", value = "/CookieDemo1") public class CookieDemo1 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 c = new Cookie("msg", "hello"); //2.发送Cookie response.addCookie(c); } }
获取遍历Cookie:
@WebServlet(name = "CookieDemo2", value = "/CookieDemo2") public class CookieDemo2 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 { //3.获取Cookie Cookie[] cookies = request.getCookies(); //获取数据,遍历Cookie if (cookies != null){ for (Cookie cookie : cookies) { String name = cookie.getName(); String value = cookie.getValue(); System.out.println(name+":"+value); } } } }
标签:入门,request,value,会话,Cookie,response,name From: https://www.cnblogs.com/qihaokuan/p/16969931.html