JavaWeb16.2【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  *  一次可以发送多个cookie
14  */
15 @WebServlet("/CookieDemo3")
16 public class CookieDemo3 extends HttpServlet {
17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         Cookie c1 = new Cookie("msg1", "hello");
19         Cookie c2 = new Cookie("msg2", "你好");
20         response.addCookie(c1);
21         response.addCookie(c2);
22 
23         /*
24         同一浏览器内,按顺序访问
25         http://localhost:8080/day16/CookieDemo3
26         http://localhost:8080/day16/CookieDemo2
27 
28         msg1:hello
29         msg2:你好
30          */
31     }
32 
33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34         this.doPost(request, response);
35     }
36 }

 

 

 

 

 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  *   cookie在浏览器中的保存时间
14  */
15 @WebServlet("/CookieDemo4")
16 public class CookieDemo4 extends HttpServlet {
17     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         Cookie c1 = new Cookie("msg", "setMaxAge");
19 
20         //设置Cookie的存活时间
21 //        c1.setMaxAge(30); //将cookie持久化到硬盘,30秒后会自动删除cookie文件
22 //        c1.setMaxAge(-1); //-1是默认值,即当浏览器关闭后,Cookie数据被销毁
23         /*c1.setMaxAge(300);
24         c1.setMaxAge(0); //删除Cookie*/
25 
26         response.addCookie(c1);
27     }
28 
29     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30         this.doPost(request, response);
31     }
32 }
 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  *   tomcat8之后,cookie能存中文
14  *   cookie共享问题
15  */
16 @WebServlet("/CookieDemo5")
17 public class CookieDemo5 extends HttpServlet {
18     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19         /*Cookie c = new Cookie("msg", "你好吗?");
20         response.addCookie(c);*/
21 
22         Cookie c = new Cookie("msg", "你好吗?");
23 //        c.setPath("/day16"); //setPath默认情况即为当前项目的虚拟目录,此时cookie不能共享
24         c.setPath("/"); //这样设置,同一个tomcat服务器中的多个web项目就可以共享了
25         response.addCookie(c);
26     }
27 
28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29         this.doPost(request, response);
30     }
31 }

 

posted @ 2021-07-02 16:49  yub4by  阅读(42)  评论(0编辑  收藏  举报