JavaWeb16.1【Cookie&Session:会话技术概述、Cookie快速入门和实现原理】
1 package com.haifei.cookie; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.Cookie; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * Cookie快速入门 13 */ 14 @WebServlet("/CookieDemo1") 15 public class CookieDemo1 extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 //1. 创建Cookie对象,绑定数据 18 Cookie c = new Cookie("msg", "hello"); 19 //2. 发送Cookie对象 20 response.addCookie(c); 21 } 22 23 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 this.doPost(request, response); 25 } 26 } 27 28 29 //修改IDEA生成servlet代码的模板(File->Settings->File and code template) 30 /* 默认模板 31 #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end 32 #parse("File Header.java") 33 @javax.servlet.annotation.WebServlet(name = "${Entity_Name}") 34 public class ${Class_Name} extends javax.servlet.http.HttpServlet { 35 protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { 36 } 37 protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { 38 } 39 } 40 */ 41 42 /* 修改模板 43 #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end 44 #parse("File Header.java") 45 @javax.servlet.annotation.WebServlet("/${Entity_Name}") 46 public class ${Class_Name} extends javax.servlet.http.HttpServlet { 47 protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { 48 } 49 protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException { 50 this.doPost(request, response); 51 } 52 } 53 */
1 package com.haifei.cookie; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.Cookie; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 /** 12 * Cookie快速入门 13 */ 14 @WebServlet("/CookieDemo2") 15 public class CookieDemo2 extends HttpServlet { 16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 //3. 获取Cookie 18 Cookie[] cs = request.getCookies(); 19 //遍历Cookies,拿到数据 20 if (cs != null){ 21 for (Cookie c : cs) { 22 String name = c.getName(); 23 String value = c.getValue(); 24 System.out.println(name + ":" + value); 25 } 26 } 27 28 /* 29 同一浏览器内,按顺序访问 30 http://localhost:8080/day16/CookieDemo1 31 http://localhost:8080/day16/CookieDemo2 32 33 msg:hello 34 35 // JSESSIONID:A80FF0263EF46C8D236A7E98D8B9CF05 36 // msg:hello 37 // Pycharm-bef28cc3:6a7e18fa-9ae4-419f-9f06-22bb003caf2b 38 // _ga:GA1.1.1972130967.1563781280 39 // _pk_id.100001.1fff:dafa65dff09610aa.1563781279.2.1568286400.1563781584. 40 // __utma:111872281.1972130967.1563781280.1563781280.1568286412.2 41 // Idea-d3ad9769:02eef562-1b85-4098-a324-4ed6e79f0a1f 42 */ 43 44 /* 45 新浏览器直接访问 46 http://localhost:8080/day16/CookieDemo2 47 48 无输出 49 */ 50 } 51 52 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53 this.doPost(request, response); 54 } 55 }