Skip to content

Commit 7de0e99

Browse files
committed
update
1 parent fff08e4 commit 7de0e99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2171
-11
lines changed

.idea/artifacts/vehicle_rent_war.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/artifacts/vehicle_rent_war_exploded.xml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/vehicle_rent_lib.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/vehicle_rent_lib__2_.xml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.atguigu.servlet;
22

3+
import com.atguigu.util.CookieUtils;
4+
35
import javax.servlet.ServletException;
46
import javax.servlet.http.Cookie;
57
import javax.servlet.http.HttpServletRequest;
@@ -9,12 +11,130 @@
911
public class CookieServlet extends BaseServlet {
1012

1113

14+
/**存活一小时的cookie
15+
*
16+
* @param req
17+
* @param resp
18+
* @throws ServletException
19+
* @throws IOException
20+
*/
21+
protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22+
Cookie cookie=new Cookie("life3600","life3600");
23+
cookie.setMaxAge(60*60);
24+
resp.addCookie(cookie);
25+
resp.getWriter().write("存活一小时的cookie");
26+
}
27+
28+
29+
30+
31+
/** 退出浏览器,就被删除的cookie(默认 会话级别的cookie)
32+
*
33+
* @param req
34+
* @param resp
35+
* @throws ServletException
36+
* @throws IOException
37+
*/
38+
protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
39+
Cookie cookie=new Cookie("defaultLife","defaultLife");
40+
cookie.setMaxAge(-1);
41+
resp.addCookie(cookie);
42+
}
43+
44+
45+
46+
/** 立即删除指定cookie
47+
*
48+
* @param req
49+
* @param resp
50+
* @throws ServletException
51+
* @throws IOException
52+
*/
53+
protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
54+
//1.先找到要删除的 cookie
55+
Cookie cookie=CookieUtils.findCookie("key3",req.getCookies());
56+
if(cookie!=null){
57+
//2.调用 setMaxAge(0)
58+
cookie.setMaxAge(0);
59+
}
60+
//3.调用 addCookie()
61+
resp.addCookie(cookie);
62+
63+
resp.getWriter().write("key3的cookie已经被删除");
64+
}
65+
66+
67+
/** 更新cookie的value
68+
*
69+
* @param req
70+
* @param resp
71+
* @throws ServletException
72+
* @throws IOException
73+
*/
74+
protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
75+
// //1.方案一: 创建 一个新的 同名 Cookie , 覆盖 旧的就行了
76+
// Cookie cookies=new Cookie("key1","newValue1");
77+
// resp.addCookie(cookies);
78+
// resp.getWriter().write("key1的Cookie修改好了");
79+
80+
//2.先找到需要的Cookie ,再修改
81+
Cookie cookie=CookieUtils.findCookie("key2",req.getCookies());
82+
if(cookie!=null){
83+
cookie.setValue("newValue2");
84+
}
85+
//通知客户端保存 cookie
86+
resp.addCookie(cookie);
87+
}
88+
89+
/** 客户端 获取 服务器 Cookie的方法
90+
* 先获取所有cookie,再遍历查找自己需要的
91+
* @param req
92+
* @param resp
93+
* @throws ServletException
94+
* @throws IOException
95+
*/
96+
protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
97+
Cookie[] cookies = req.getCookies();
98+
99+
for (Cookie cookie : cookies) {
100+
resp.getWriter().write("Cookie[" + cookie.getName() + "=" + cookie.getValue() + "]<br/>");
101+
}
102+
Cookie iWantCookie= CookieUtils.findCookie("key2",cookies);//左参数 为查找目标,右参数为 所有cookie的集合
103+
104+
// for (Cookie cookie : cookies) {
105+
// if("key2".equals(cookie.getName())){
106+
// iWantCookie=cookie;
107+
// break;
108+
// }
109+
// }
110+
if(iWantCookie!=null){
111+
resp.getWriter().write("找到了需要的Cookie");
112+
}
113+
114+
}
115+
116+
/** 创建cookie
117+
*
118+
* @param req
119+
* @param resp
120+
* @throws ServletException
121+
* @throws IOException
122+
*/
12123
protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13124
//1.创建cookie对象
14-
Cookie cookie=new Cookie("key1","value1");
125+
Cookie cookie1 = new Cookie("key1", "value1");
15126
//2.通知客户端 保存cookie对象
16-
resp.addCookie(cookie);
127+
resp.addCookie(cookie1);
128+
129+
//1.创建cookie对象
130+
Cookie cookie2 = new Cookie("key2", "value2");
131+
//2.通知客户端 保存cookie对象
132+
resp.addCookie(cookie2);
17133

134+
//1.创建cookie对象
135+
Cookie cookie3 = new Cookie("key3", "value3");
136+
//2.通知客户端 保存cookie对象
137+
resp.addCookie(cookie3);
18138
resp.getWriter().write("Cookie对象创建成功");
19139
}
20140
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.atguigu.util;
2+
3+
import javax.servlet.http.Cookie;
4+
5+
public class CookieUtils {
6+
7+
/**
8+
*
9+
* @param name 想查找的cookie名字
10+
* @param cookies 所有cookie组成的集合 , 一般用req.getCookies()找到所有cookie
11+
* @return
12+
*/
13+
public static Cookie findCookie(String name,Cookie[] cookies){
14+
if(name==null||cookies==null||cookies.length==0 ){
15+
return null;
16+
}
17+
18+
for (Cookie cookie : cookies) {
19+
if(name.equals(cookie.getName())){
20+
return cookie;
21+
}
22+
}
23+
return null;
24+
}
25+
}

0 commit comments

Comments
 (0)