Skip to content

Commit 9c5f457

Browse files
committed
update
1 parent 7de0e99 commit 9c5f457

File tree

8 files changed

+170
-7
lines changed

8 files changed

+170
-7
lines changed

_13_cookie_session/src/main/java/com/atguigu/servlet/CookieServlet.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
public class CookieServlet extends BaseServlet {
1212

1313

14+
protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15+
Cookie cookie=new Cookie("path1","path1");
16+
// req.getContextPath() 获取工程路径
17+
cookie.setPath(req.getContextPath()+"/abc");// ===>>> 设置地址: /工程路径/abc
18+
resp.addCookie(cookie);
19+
resp.getWriter().write("创建了一个带有Path路径的Cookie");
20+
}
21+
1422
/**存活一小时的cookie
1523
*
1624
* @param req
@@ -78,6 +86,7 @@ protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) th
7886
// resp.getWriter().write("key1的Cookie修改好了");
7987

8088
//2.先找到需要的Cookie ,再修改
89+
//传入待查找的cookie的key 和 cookie集合 然后找到了就用新的value覆盖旧的value
8190
Cookie cookie=CookieUtils.findCookie("key2",req.getCookies());
8291
if(cookie!=null){
8392
cookie.setValue("newValue2");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.atguigu.servlet;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.http.Cookie;
5+
import javax.servlet.http.HttpServlet;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.io.IOException;
9+
10+
public class LoginServlet extends HttpServlet {
11+
12+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
13+
String username=req.getParameter("username");
14+
String password = req.getParameter("password");
15+
16+
//检查用户名和密码, 如果正确就登录
17+
if ("wzg168".equals(username)&&"123456".equals(password)){
18+
//用cookie保存当前用户名
19+
Cookie cookie = new Cookie("username", "wzg168");
20+
cookie.setMaxAge(60*60*24*7);//保存7天记录
21+
resp.addCookie(cookie);
22+
System.out.println("登录成功");
23+
}else{
24+
System.out.println("登录失败");
25+
}
26+
}
27+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.atguigu.servlet;
2+
3+
import javax.servlet.ServletException;
4+
import javax.servlet.http.HttpServletRequest;
5+
import javax.servlet.http.HttpServletResponse;
6+
import javax.servlet.http.HttpSession;
7+
import java.io.IOException;
8+
9+
public class SessionServlet extends BaseServlet{
10+
11+
/** 往session域中保存保存数据
12+
*
13+
* @param req
14+
* @param resp
15+
* @throws ServletException
16+
* @throws IOException
17+
*/
18+
protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19+
//getSession()第一次是创建Session,之后调用都是获取之前创建的Session
20+
req.getSession().setAttribute("key1","value1");
21+
resp.getWriter().write("已经往Session域中保存了数据");
22+
}
23+
24+
/** 从Session域中获取数据
25+
*
26+
* @param req
27+
* @param resp
28+
* @throws ServletException
29+
* @throws IOException
30+
*/
31+
protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
32+
Object attribute= req.getSession().getAttribute("key1");
33+
resp.getWriter().write("从Session域中获取到的key1的数据是:"+attribute);
34+
}
35+
36+
/** 创建或获取 Session
37+
*
38+
* @param req
39+
* @param resp
40+
* @throws ServletException
41+
* @throws IOException
42+
*/
43+
protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
44+
//创建和获取Session会话对象
45+
HttpSession session=req.getSession();
46+
//判断当前session是否是新创建的
47+
boolean isNew=session.isNew();
48+
//获取Session唯一标识id
49+
String id= session.getId();
50+
resp.getWriter().write("得到的Session,它的id是:"+id +"<br/>");
51+
resp.getWriter().write("这个Session是否是新创建的:"+isNew +"<br/>");
52+
53+
}
54+
55+
56+
57+
protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
58+
int maxInactiveInterval = req.getSession().getMaxInactiveInterval();//默认生命周期
59+
//这里超时时长是1800秒即30分钟 ,因为tomcat的web.xml文件里 有默认配置:
60+
/*
61+
<session-config>
62+
<session-timeout>30</session-timeout>
63+
</session-config>
64+
*/
65+
//如果要改的话,直接在idea的xml文件里改就行 ,单独作用于当前xml的话就利用上面的api,getMaxInactiveInterval(int )
66+
67+
resp.getWriter().write("Session的默认超时时长为"+maxInactiveInterval+ "秒");
68+
69+
}
70+
protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
71+
HttpSession session = req.getSession();
72+
req.getSession().setMaxInactiveInterval(3);
73+
resp.getWriter().write("已经设置超时时长为3秒");
74+
}
75+
76+
protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
77+
HttpSession session = req.getSession();
78+
req.getSession().invalidate();
79+
resp.getWriter().write("已经设置session为无效");
80+
}
81+
82+
}

_13_cookie_session/src/main/webapp/WEB-INF/web.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,26 @@
1212
<servlet-name>CookieServlet</servlet-name>
1313
<url-pattern>/cookieServlet</url-pattern>
1414
</servlet-mapping>
15+
16+
<servlet>
17+
<servlet-name>LoginServlet</servlet-name>
18+
<servlet-class>com.atguigu.servlet.LoginServlet</servlet-class>
19+
</servlet>
20+
<servlet-mapping>
21+
<servlet-name>LoginServlet</servlet-name>
22+
<url-pattern>/loginServlet</url-pattern>
23+
</servlet-mapping>
24+
25+
<servlet>
26+
<servlet-name>SessionServlet</servlet-name>
27+
<servlet-class>com.atguigu.servlet.SessionServlet</servlet-class>
28+
</servlet>
29+
<servlet-mapping>
30+
<servlet-name>SessionServlet</servlet-name>
31+
<url-pattern>/sessionServlet</url-pattern>
32+
</servlet-mapping>
33+
34+
<session-config>
35+
<session-timeout>40</session-timeout>
36+
</session-config>
1537
</web-app>

_13_cookie_session/src/main/webapp/cookie.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<li><a href="cookieServlet?action=life3600" target="target">Cookie存活3600秒(1小时)</a></li>
3232
</ul>
3333
</li>
34-
<li><a href="" target="target">Cookie的路径设置</a></li>
34+
<li><a href="cookieServlet?action=testPath" target="target">Cookie的路径设置</a></li>
3535
<li><a href="" target="target">Cookie的用户免登录练习</a></li>
3636
</ul>
3737
</div>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: 强风吹拂
4+
Date: 2023/6/21
5+
Time: 15:06
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<html>
10+
<head>
11+
<title>Title</title>
12+
</head>
13+
<body>
14+
<form action="http://localhost:8080/_13_cookie_session/loginServlet" method="get">
15+
用户名 : <input type="text" name="username" value="${cookie.username.value}"><br>
16+
密码 : <input type="password" name="password" ><br>
17+
<input type="submit" value="登录">
18+
19+
</form>
20+
</body>
21+
</html>

_13_cookie_session/src/main/webapp/session.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313
}
1414

1515
</style>
16+
<base href="http://localhost:8080/_13_cookie_session/">
1617
</head>
1718
<body>
1819
<iframe name="target" width="500" height="500" style="float: left;"></iframe>
1920
<div style="float: left;">
2021
<ul>
21-
<li><a href="" target="target">Session的创建和获取(id号、是否为新创建)</a></li>
22-
<li><a href="" target="target">Session域数据的存储</a></li>
23-
<li><a href="" target="target">Session域数据的获取</a></li>
22+
<li><a href="sessionServlet?action=createOrGetSession" target="target">Session的创建和获取(id号、是否为新创建)</a></li>
23+
<li><a href="sessionServlet?action=setAttribute" target="target">Session域数据的存储</a></li>
24+
<li><a href="sessionServlet?action=getAttribute" target="target">Session域数据的获取</a></li>
2425
<li>Session的存活</li>
2526
<li>
2627
<ul>
27-
<li><a href="" target="target">Session的默认超时及配置</a></li>
28-
<li><a href="" target="target">Session3秒超时销毁</a></li>
29-
<li><a href="" target="target">Session马上销毁</a></li>
28+
<li><a href="sessionServlet?action=defaultLife" target="target">Session的默认超时及配置</a></li>
29+
<li><a href="sessionServlet?action=life3" target="target">Session3秒超时销毁</a></li>
30+
<li><a href="sessionServlet?action=deleteNow" target="target">Session马上销毁</a></li>
3031
</ul>
3132
</li>
3233
<li><a href="" target="target">浏览器和Session绑定的原理</a></li>

book/src/main/java/com/atguigu/web/BookServlet.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected void page(HttpServletRequest req, HttpServletResponse resp) throws Ser
3535

3636
page.setUrl("manager/bookServlet?action=page");
3737

38+
//page.serUrl("manager/bookServlet?action=page");
3839

3940
//3.保存page对象到Request域中
4041
req.setAttribute("page",page);

0 commit comments

Comments
 (0)