一、Cookie
1.介绍:
1)Cookie来自于Servlet规范中一个工具类,存在于Tomcat提供servlet-api.jar中
2)如果两个Servlet来自于同一个网站,并且为同一个浏览器/用户提供服务,此时
借助于Cookie对象进行数据共享
3) Cookie存放当前用户的私人数据,在共享数据过程中提高服务质量
4) 在现实生活场景中,Cookie相当于用户在服务端得到【会员卡】
2.原理:
用户通过浏览器第一次向MyWeb网站发送请求申请OneServlet。
OneServlet在运行期间创建一个Cookie存储与当前用户相关数据
OneServlet工作完毕后,【将Cookie写入到响应头】交还给当前
浏览器。
浏览器收到响应响应包之后,将cookie存储在浏览器的缓存
一段时间之后,用户通过【同一个浏览器】再次向【myWeb网站】发送请求申请TwoServlet 时。
【浏览器需要无条件的将myWeb网站之前推送过来的Cookie,写入到请求头】发送过去
此时TwoServlet在运行时,就可以通过读取请求头中cookie中信息,得到OneServlet提供的
共享数据
3.实现命令:
同一个网站 OneServlet 与 TwoServlet 借助于Cookie实现数据共享
OneServlet{
public void doGet(HttpServletRequest request,HttpServletResponse resp){
//1.创建一个cookie对象,保存共享数据(当前用户数据)
Cookie card = new Cookie("key1","abc");
Cookie card1= new Cookie("key2","efg");
****cookie相当于一个map
****一个cookie中只能存放一个键值对
****这个键值对的key与value只能是String
****键值对中key不能是中文
//2.【发卡】将cookie写入到响应头,交给浏览器
resp.addCookie(card);
resp.addCookie(card1)
}
}
浏览器/用户 <---------响应包 【200】
【cookie: key1=abc; key2=eft】
【】
【处理结果】
浏览器向myWeb网站发送请求访问TwoServlet---->请求包 【url:/myWeb/two method:get】
【
请求参数:xxxx
Cookie key1=abc;key2=efg
】
【】
【】
TwoServlet{
public void doGet(HttpServletRequest request,HttpServletResponse resp){
//1.调用请求对象从请求头得到浏览器返回的Cookie
Cookie cookieArray[] = request.getCookies();
//2.循环遍历数据得到每一个cookie的key 与 value
for(Cookie card:cookieArray){
String key = card.getName(); 读取key "key1"
Strign value = card.getValue();读取value "abc"
提供较好的服务。。。。。。。。
}
}
}
4.Cookie销毁时机:
1.在默认情况下,Cookie对象存放在浏览器的缓存中。
因此只要浏览器关闭,Cookie对象就被销毁掉
2.在手动设置情况下,可以要求浏览器将接收的Cookie
存放在客户端计算机上硬盘上,同时需要指定Cookie
在硬盘上存活时间。在存活时间范围内,关闭浏览器
关闭客户端计算机,关闭服务器,都不会导致Cookie
被销毁。在存活时间到达时,Cookie自动从硬盘上被
删除
cookie.setMaxAge(60); //cookie在硬盘上存活1分钟
5.Cookie理解:
* 首先对于Cookie,cookie是不安全的,可以人为修改
* 需要Cookie的页面如果没有得到Cookie参数,就会报500错
* Cookie会在关闭浏览器的时候销毁
6.Cookie实例——订餐会员卡
oneServlet
public class oneServlet extends javax.servlet.http.HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用请求对象读取【请求参数】信息
String userName, money;
userName = request.getParameter("userName");
money = request.getParameter("money");
//2.开卡
Cookie card1 = new Cookie("userName", userName);
Cookie card2 = new Cookie("money", money);
//3.发卡,将Cookie写入到响应头交给浏览器
response.addCookie(card1);
response.addCookie(card2);
//4.通知浏览器将点餐页面内容写入到响应体交给浏览器(请求转发)
request.getRequestDispatcher("/index_2.html").forward(request, response);
}
}
twoServlet
/**
* Created by IntelliJ IDEA.
* User: LvHaoIT (asus)
* Date: 2021/5/5
* Time: 14:14
*/
public class twoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
double jiaozi_money = 30;
double gaifan_money = 21;
double miantiao_money = 20;
double money = 0;
double xiaofei = 0;
String food, userName = null;
//修改响应体字符集
response.setContentType("text/html;charset=utf-8");
//获取输出流对象
PrintWriter out = response.getWriter();
//定义一个新的Cookie,用新处理的值覆盖以前的cookie
Cookie newCard = null;
//1.读取请求头参数信息,得到用户点餐的食物类型
food = request.getParameter("food");
//2。读取请求中的Cookie
Cookie[] cookieArray = request.getCookies();//取出所有cookie
//3.刷卡消费
for (Cookie card : cookieArray) {
//取出数据并且对应处理
String key = card.getName();//获取key
String value = card.getValue();//获取value
//判断取出的是否为用户名
if (key.equals("userName")) {
//是用户名
userName = value;//找到对应用户名
} else if (key.equals("money")) {//取出的是money
money = Double.valueOf(value); //将钱存起来
//开始消费 food里存的是食物名称
if (food.equals("饺子")) {
//点的是饺子,但是需要判断钱够不够
if (money < jiaozi_money) {
out.print("用户" + userName + "余额不足,请充值!");
} else {
//钱够,可以消费
newCard = new Cookie("money", (money - jiaozi_money) + "");
xiaofei = jiaozi_money;
}
} else if (food.equals("面条")) {
//点的是饺子,但是需要判断钱够不够
if (money < jiaozi_money) {
out.print("用户" + userName + "余额不足,请充值!");
} else {
//钱够,可以消费
newCard = new Cookie("money", (money - miantiao_money) + "");
xiaofei = miantiao_money;
}
} else if (food.equals("盖饭")) {
//点的是饺子,但是需要判断钱够不够
if (money < jiaozi_money) {
out.print("用户" + userName + "余额不足,请充值!");
} else {
//钱够,可以消费
newCard = new Cookie("money", (money - gaifan_money) + "");
xiaofei = gaifan_money;
}
}
}
}
//4.返还用户会员卡
response.addCookie(newCard);
//5.消费记录写入到响应对象中
out.println("用户" + userName + "本次消费:" + xiaofei + " 卡内余额剩余:" + (money - xiaofei));
}
}
测试网页:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新用户开卡</title>
</head>
<body>
<center>
<font style="color: red;font-size:40px ;">新会员开卡</font>
<form action="/myCookie/one" method="GET">
<table border="2">
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="userName"/>
</td>
</tr>
<tr>
<td>
预存金额:
</td>
<td>
<input type="text" name="money"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="申请开卡"/>
</td>
<td>
<input type="reset" name="重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
index_2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点餐页面</title>
</head>
<body>
<center>
<font style="color: red;font-size: 40px;">点餐页面</font>
<form action="/myCookie/two">
食物类型:
<input type="radio" name="food" value="饺子" />饺子(30元)<br>
<input type="radio" name="food" value="面条" />面条(20元)<br>
<input type="radio" name="food" value="盖饭" />盖饭(21元)<br>
<input type="submit" value="会员卡消费" />
</form>
</center>
</body>
</html>
实现原理图
二、HttpSession接口:
1.介绍:
1)HttpSession接口来自于Servlet规范下一个接口。存在于Tomcat中servlet-api.jar
其实现类由Http服务器提供。Tomcat提供实现类存在于servlet-api.jar
2)如果两个Servlet来自于同一个网站,并且为同一个浏览器/用户提供服务,此时
借助于HttpSession对象进行数据共享
3)开发人员习惯于将HttpSession接口修饰对象称为【会话作用域对象】
2.HttpSession 与 Cookie 区别:【面试题】
1)存储位置: 一个在天上,一个在地下
Cookie:存放在客户端计算机(浏览器内存/硬盘)
HttpSession:存放在服务端计算机内存
2)数据类型:
Cookie对象存储共享数据类型只能是String
HttpSession对象可以存储任意类型的共享数据Object
3) 数据数量:
一个Cookie对象只能存储一个共享数据
HttpSession使用map集合存储共享数据,所以可以
存储任意数量共享数据
4)参照物:
Cookie相当于客户在服务端【会员卡】
HttpSession相当于客户在服务端【私人保险柜】
3.命令实现: 同一个网站(myWeb)下OneServlet将数据传递给TwoServlet
OneServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response){
//1.调用请求对象向Tomcat索要当前用户在服务端的私人储物柜
HttpSession session = request.getSession();
//2.将数据添加到用户私人储物柜
session.setAttribute("key1",共享数据)
}
}
浏览器访问/myWeb中TwoServlet
TwoServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response){
//1.调用请求对象向Tomcat索要当前用户在服务端的私人储物柜
HttpSession session = request.getSession();
//2.从会话作用域对象得到OneServlet提供的共享数据
Object 共享数据 = session.getAttribute("key1");
}
}
4.Http服务器如何将用户与HttpSession关联起来
cookie
**5.getSession() 与 getSession(false)**区别
1)getSession(): 如果当前用户在服务端已经拥有了自己的私人储物柜.
要求tomcat将这个私人储物柜进行返回
如果当前用户在服务端尚未拥有自己的私人储物柜
要求tocmat为当前用户创建一个全新的私人储物柜
2)getSession(false):如果当前用户在服务端已经拥有了自己的私人储物柜.
要求tomcat将这个私人储物柜进行返回
如果当前用户在服务端尚未拥有自己的私人储物柜
此时Tomcat将返回null
6.HttpSession销毁时机:
1.用户与HttpSession关联时使用的Cookie只能存放在浏览器缓存中.
2.在浏览器关闭时,意味着用户与他的HttpSession关系被切断
3.由于Tomcat无法检测浏览器何时关闭,因此在浏览器关闭时并不会
导致Tomcat将浏览器关联的HttpSession进行销毁
4.为了解决这个问题,Tomcat为每一个HttpSession对象设置【空闲时间】
这个空闲时间默认30分钟,如果当前HttpSession对象空闲时间达到30分钟
此时Tomcat认为用户已经放弃了自己的HttpSession,此时Tomcat就会销毁
掉这个HttpSession
7.HttpSession空闲时间手动设置
在当前网站/web/WEB-INF/web.xml
<session-config>
<session-timeout>5</session-timeout>
<!--当前网站中每一个session最大空闲时间5分钟-->
</session-config>
实现实例
oneServlet
/**
* Created by IntelliJ IDEA.
* User: LvHaoIT (asus)
* Date: 2021/5/5
* Time: 16:59
*/
public class oneServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用请求对象,读取请求头参数
String goodsName;
request.setCharacterEncoding("utf-8");
goodsName = request.getParameter("goodsName");
System.out.println("进入了断点1!!");
//2.调用请求对象,向tomcat索要私人储物柜
HttpSession session = request.getSession();
//3.将用户选购商品添加到当前私人储物柜
Integer goodsNum = (Integer) session.getAttribute(goodsName);//取商品数量
//判断商品是否是第一次放入
if (goodsNum == null) {
session.setAttribute(goodsName, 1);
} else {
//存在过,则加一
session.setAttribute(goodsName, goodsNum + 1);
}
}
}
twoServlet
/**
* Created by IntelliJ IDEA.
* User: LvHaoIT (asus)
* Date: 2021/5/5
* Time: 17:11
*/
public class twoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用请求对象,向tomcat索要当前用户在服务端的私人储物柜
HttpSession session = request.getSession();
System.out.println("进入了断点2!!");
//2.将session中所有的key取出来,存放在一个枚举对象中
Enumeration goodNames = session.getAttributeNames();
//hasMoreElements()当且仅当此枚举对象至少还包含一个可提供的元素时,才返回 true;否则返回 false。
while (goodNames.hasMoreElements()) {
//E nextElement()
//如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。
String goodName = (String) goodNames.nextElement();//取出下一个商品名
int goodsNum = (int) session.getAttribute(goodName);//取出对应商品名称的件数
System.out.println("商品名称:" + goodName + "商品数量:" + goodsNum);
}
}
}
测试网页
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="2" align="center">
<tr>
<td>商品名称</td>
<td>商品单价</td>
<td>供货商</td>
<td>放入购物车</td>
</tr>
<tr>
<td>华为笔记本电脑pro13</td>
<td>7000</td>
<td>华为</td>
<td><a href="/myweb/one?goodsName=华为笔记本电脑pro13">放入购物车</a></td>
</tr>
<tr>
<td>榴莲</td>
<td>200</td>
<td>泰国</td>
<td><a href="/myweb/one?goodsName=榴莲">放入购物车</a></td>
</tr>
<tr>
<td>男士内裤</td>
<td>1000</td>
<td>朱方成</td>
<td><a href="/myweb/one?goodsName=男士内裤">放入购物车</a></td>
</tr>
<tr align="center">
<td colspan="4">
<a href="/myweb/two">查看我的购物车</a>
</td>
</tr>
</table>
</body>
</html>
实现原理图: