您的位置:首页 > 编程语言 > Java开发

11-Javaweb-cookie & session

2020-07-14 05:55 267 查看

目录

一、案例1-记录用户上次访问时间

1-1 jsp

1-1-1 jsp执行流程

1-1-2 jsp的脚本        

1-2 会话技术

1-3 会话技术分类

1-4 cookie

 1-4-1 cookie获取写回常用方法

1-5 案例1-步骤分析

1-5-1 创建一个serlvet RemServlet 路径:/rem

1-5-2 在servlet中实现cookie记录上次访问时间

1-6 cookie-总结

1-6-1 持久化常用方法

二、案例2-记录用户浏览历史

2-1 步骤分析

2-1-1 先将product_list.htm转成jsp

2-1-2 点击一个商品,展示该商品的信息,将该商品id记录到cookie  (GetProductByIdServlet  类中)

2-1-3 再次回到product_list.jsp页面,需要将之前访问商品展示在浏览记录中

2-2 扩展:删除浏览记录

2-2-1 在浏览器记录中添加一个超链接 

2-2-2 创建servlet clearHistroy

2-2-3 页面跳转

 2-3 cookie 注意

三、案例3-添加到购物车

3-1 session

3-1-1 获取一个session

3-1-2 域对象

3-2 步骤分析

3-2-1 点击添加到购物车的时候,提交到一个servlet add2CartServlet

3-2-2 add2CartServlet中的操作

3-2-3 点击购物车连接的时候 cart.jsp

3-3 案例3-扩展清空购物车

3-3-1 步骤分析

总结

 

  • 一、案例1-记录用户上次访问时间

需求:
        当用户第一次登录的时候,提示:你是第一次访问,且记录该次访问时间,
        下一次访问的时候,获取上一次访问时间且展示出来
技术分析:
        会话技术
        cookie
        jsp

  • 1-1 jsp

        java server pages(java服务器页面)
        本质上jsp就是一个servlet,在html代码中嵌套java代码,——继承并实现了httpservlet
        运行在服务器端,处理请求,生成动态的内容.
        对应的java和class文件在tomcat目录下的work目录
        后缀名 .jsp

        修改默认编码

  • 1-1-1 jsp执行流程

        1.浏览器发送请求,访问jsp页面

        2.服务器接受请求,jspSerlvet会帮我们查找对应的jsp文件(后缀名匹配)

        3.服务器将jsp页面翻译成.java文件.

        4.jvm会将.java编译成.class文件

        5.服务器运行class文件,生成动态的内容.

        6.将内容发送给服务器,

        7.服务器组成响应信息,发送给浏览器

        8.浏览器接受数据,解析展示

  • 1-1-2 jsp的脚本        

        <%...%> java程序片段
                生成成jsp的service方法中        _jspService()
        <%=...%> 输出表达式
                生成成jsp的service方法中,相当于在java中调用out.print(..)      out——内置对象
        <%!...%> 声明成员
                成员位置.

[code]<body>
hello jsp!你好
<%
int i=3;
System.out.println(i);//在控制台输出结果
%>
<%=i %>  <!--相当于调用  out.print("我很好");-->
<%
out.print("我很好");
%>
<%=k %>
<%!
int k=4;//定义
%>
<%=k %>
</body>
  • 1-2 会话技术

        当用户打开浏览器的时候,访问不同的资源,直到用户将浏览器关闭,可以认为这是一次会话.                
        作用:
                因为http协议是一个无状态的协议,它记录不论上次访问的内容.用户在访问过程中难免会产生一些数据,
                通过会话技术就可以将起保存起来.
        例如:
                用户登录
                验证码
                购物车
                访问记录
                .....

  • 1-3 会话技术分类

        cookie:浏览器端会话技术
        session:服务器端会话技术

  • 1-4 cookie

        小饼干 小甜点
        cookie是由服务器生成,通过response将cookie写回浏览器(set-cookie),保留在浏览器上,
        下一次访问,浏览器根据一定的规则携带不同的cookie(通过request的头 cookie),我们服务器就可以接受cookie

 

  •  1-4-1 cookie获取写回常用方法

      cookie的api

                new Cookie(String key,String value)

        写回浏览器:

                response.addCookie(Cookie c)

        获取cookie:

                Cookie[] request.getCookies()

        cookie的常用方法:

                getName():获取cookie的key(名称)

                getValue:获取指定cookie的值

[code]/**
* cookie入门
*/
public class HelloCookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
response.setContentType("text/html;charset=utf-8");
PrintWriter w = response.getWriter();

//创建一个cookie
Cookie a=new Cookie("akey", "avalue");
Cookie b=new Cookie("bkey", "bvalue");
Cookie c=new Cookie("ckey", "cvalue");

//写回浏览器
response.addCookie(a);
response.addCookie(b);
response.addCookie(c);

//提示信息
w.print("cookie 已写回");;
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
  • 1-5 案例1-步骤分析

  • 1-5-1 创建一个serlvet RemServlet 路径:/rem

  • 1-5-2 在servlet中实现cookie记录上次访问时间

        1 获取指定cookie 例如:名称为 lastTime     

                request.getCookies()
        2 判断cookie是否为空
                若为空:提示信息 第一次访问
                若不为空:
                        获取此cookie的value
                        展示信息:你上次访问时间是....
        
        3-1 将这次访问时间记录

        3-2 写回浏览器

[code]/**
* 记录上次访问时间
*/
public class RemServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//0.设置编码
response.setContentType("text/html;charset=utf-8");
PrintWriter w = response.getWriter();

//1.获取指定名称的cookie——自定义方法获取最后一个cookie
Cookie c=getCookieByName("lastTime",request.getCookies());

//2.判断cookie是否为空
if(c == null){
//cookie为空 提示 第一次访问
w.print("您是第一次访问!");
}else{
//cookie不为空  获取value 展示 上一次访问的时间
String value = c.getValue();// lastTime=12312324234
long time = Long.parseLong(value);
Date date = new Date(time);
w.print("您上次访问时间:"+date.toLocaleString());
}

//3.将当前访问时间记录
//3.1创建cookie
c=new Cookie("lastTime",new Date().getTime()+"");

//持久化cookie
c.setMaxAge(3600);
//设置路径
c.setPath(request.getContextPath()+"/");//  /day11/

//3.2写回浏览器
response.addCookie(c);
}

/**
* 通过名称在cookie数组获取指定的cookie
* @param name cookie名称
* @param cookies  cookie数组
* @return
*/
private Cookie getCookieByName(String name, Cookie[] cookies) {
if(cookies!=null){
for (Cookie c : cookies) {
//通过名称获取
if(name.equals(c.getName())){
//返回
return c;
}
}
}
return null;
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
  • 1-6 cookie-总结

  • 1-6-1 持久化常用方法

        setMaxAge(int 秒):设置cookie在浏览器端存活时间  以秒为单位

                若设置成 0:删除该cookie(前提必须路径一致)

        setPath(String path):设置cookie的路径.

                当我们访问的路径中包含此cookie的path,则携带
                默认路径: 
                        访问serlvet的路径,从"/项目名称"开始,到最后一个"/"结束
                        例如:
                                访问的serlvet路径:
                                        /day11/a/b/hello
                                默认路径为:
                                        /day11/a/b
                手动设置路径:以"/项目名"开始,以"/"结尾;

  • 二、案例2-记录用户浏览历史

需求:
        当用户访问一个商品的时候,需要将该商品保留在浏览记录中
技术分析:
        cookie

  • 2-1 步骤分析

  • 2-1-1 先将product_list.htm转成jsp

        方便 运行java代码

        1 在html页面头部添加jsp头部

        2 修改超链接中的 .html 为 .jsp

  • 2-1-2 点击一个商品,展示该商品的信息,将该商品id记录到cookie  (GetProductByIdServlet  类中)

        实现点击商品  提交id ——将超链接转到  GetProductById  (servlet)  如:<a href="/day1101/getProductById?id=1">

         由servlet实现 重定向到商品页面

[code]<div class="col-md-2">
<a href="/day1101/getProductById?id=1">
<img src="products/1/cs10001.jpg" width="170" height="170" style="display: inline-block;">
</a>
<p><a href="/day1101/getProductById?id=1" style='color:green'>衣服</a></p>
<p><font color="#FF0000">商城价:&yen;299.00</font></p>
</div>

        1 获取之前的浏览记录 (cookie)例如名称:ids——指定的cookie——getCookieByName(建立工具类,方便使用)

[code]public class CookUtils {
/**
* 通过名称在cookie数组获取指定的cookie
* @param name cookie名称
* @param cookies  cookie数组
* @return
*/
public static Cookie getCookieByName(String name, Cookie[] cookies) {
if(cookies!=null){
for (Cookie c : cookies) {
//通过名称获取
if(name.equals(c.getName())){
//返回
return c;
}
}
}
return null;
}
}

        2 判断cookie是否为空
                2.1 若为空 将当前商品的id起个名称 ids 放入cookie中  ids=1
                2.2 若不为空,获取值 例如:ids=2-1(有两个商品2,1)  当前访问的id=1  使用"-"分割商品id
                        判断之前记录中有无该商品
                                若有:
                                        将当前的id放入前面  结果 ids=1-2
                                若没有:
                                        继续判断长度是否>=3
                                                若>=3,移除最后一个,将当前的id放入最前面
                                                若<3,直接将当前的id放入最前面.
                
                若 ids=3-2-1 现在访问1 结果 ids=1-3-2
                若 ids=4-3-2 现在访问1 结果 ids=1-4-3

[code]/**
* 记录商品浏览器历史
*/
public class GetProductByIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//0.设置编码
//0.1获取当前访问的商品id
String id=request.getParameter("id");

//1.获取指定的cookie ids
Cookie c = CookUtils.getCookieByName("ids", request.getCookies());

String ids="";//用于写回cookie
//2.判断cookie是否为空
if(c==null){
//若cookie为空  需要将当前商品id放入ids中
ids=id;
}else{
//若cookie不为空 继续判断ids中是否已经该id // ids=2-11-21
//获取值
ids=c.getValue();
//切割成数组
String[] arr = ids.split("-");
//将数组转成集合  此list长度不可变
List<String> asList = Arrays.asList(arr);
//将aslist放入一个新list中——经常删除
LinkedList<String> list = new LinkedList<>(asList);
//也可将  list.addFirst(id);  提出来
if(list.contains(id)){
//若ids中包含id  将id移除 放到最前面
list.remove(id);
list.addFirst(id);
}else{
//若ids中不包含id  继续判断长度是否大于2
if(list.size()>2){
//长度>=3 移除最后一个 将当前的放入最前面
list.removeLast();
list.addFirst(id);
}else{
//长度<3 将当前放入最前面
list.addFirst(id);
}
}

ids="";
//将list转成字符串
for (String s : list) {
ids+=(s+"-");
}
//去除最后一个 "-"
ids=ids.substring(0, ids.length()-1);
}

//将ids写回去
c=new  Cookie("ids",ids);
//设置访问路径
c.setPath(request.getContextPath()+"/");
//设置存活时间
c.setMaxAge(3600);

//写回浏览器
response.addCookie(c);

//3.跳转到指定的商品页面上——重定向
response.sendRedirect(request.getContextPath()+"/product_info"+id+".htm");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
  • 2-1-3 再次回到product_list.jsp页面,需要将之前访问商品展示在浏览记录中

        java代码实现

        获取ids  例如:ids=2-3-1
        切割

[code]<ul style="list-style: none;">
<%
//获取指定名称的cookie ids
Cookie c=CookUtils.getCookieByName("ids", request.getCookies());

//判断ids是否为空
if(c==null){
%>
<h2>暂无浏览记录</h2>
<%
}else{//ids=3-2-1
String[] arr=c.getValue().split("-");
for(String id:arr){
%>
<li style="width: 150px;height: 216;float: left;margin: 0 8px 0 0;padding: 0 18px 15px;text-align: center;">
<img src="products/1/cs1000<%=id %>.jpg" width="130px" height="130px" />
</li>
<%
}
}
%>
</ul>
  • 2-2 扩展:删除浏览记录

技术分析:
        cookie.setMaxAge(0)
步骤分析:

  • 2-2-1 在浏览器记录中添加一个超链接 

        <a href="/day1101/clearHistroy">清空</a>

  • 2-2-2 创建servlet clearHistroy

        创建一个cookie 
                名称路径保持一致(覆盖之前的cookie)
                setMaxAge(0)
        写回浏览器

  • 2-2-3 页面跳转

        重定向 product_list.jsp                

[code]/**
*清空浏览记录
*/
public class ClearHistroyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建一个cookie
Cookie c=new Cookie("ids", "");
c.setPath(request.getContextPath()+"/");//   /day1101/

//设置时间
c.setMaxAge(0);

//写会浏览器
response.addCookie(c);

//页面跳转
response.sendRedirect(request.getContextPath()+"/product_list.jsp");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
  •  2-3 cookie 注意

        cookie不能跨浏览器
        cookie中不支持中文——将编码后的中文存储

  • 三、案例3-添加到购物车

需求:
        在商品详情页面有一个添加到购物车,点击则将该商品添加到购物车,点击购物车连接将里面的所有商品展示出来
技术分析:
        session

  • 3-1 session

        服务器端会话技术.
        当我们第一次访问的服务器的时候,服务器获取id(cookie)
                能获取id
                        要拿着这个id去服务器中查找有无此session
                                若查找到了:直接拿过来时候,将数据保存,需要将当前sessin的id返回给浏览器
                                若查找不到:创建一个session,将你的数据保存到这个session中,将当前session的id返回给浏览器
                不能获取id
                        创建一个session,将你的数据保存到这个session中,将当前session的id返回给浏览器        

  • 3-1-1 获取一个session

        HttpSession  request.getSession()

  • 3-1-2 域对象

        xxxAttribute
        生命周期:
                创建:第一次调用request.getsession()创建
                销毁:
                        服务器非正常关闭
                        session超时
                                默认时间超时:30分钟  web.xml有配置 
                                手动设置超时:setMaxInactiveInterval(int 秒) 了解
                        手动干掉session(退出)
                                ★session.invalidate()
        存放的私有的数据.

  • 3-2 步骤分析

  • 3-2-1 点击添加到购物车的时候,提交到一个servlet add2CartServlet

        需要将商品名称携带过去   <a href="/day1101/add2Cart?name=衣服">

[code]<div style="margin:20px 0 10px 0;;text-align: center;">
<a href="/day1101/add2Cart?name=衣服">
<input
style="background: url('./images/product.gif') no-repeat scroll 0 -600px
rgba(0, 0, 0, 0);height:36px;width:127px;" value="加入购物车"
type="button">
</a> 收藏商品
</div>
  • 3-2-2 add2CartServlet中的操作

        获取商品的名称
        将商品添加到购物车 购物车的结构 Map<String 名称,Integer 购买数量>
                将购物车放入session中就可以了        
        将商品添加到购物车分析:
                获取购物车
                判断购物车是否为空
                        若为空:
                                第一次添加
                                创建一个购物车
                                将当前商品put进去.数量:1
                                将购物车放入session中
                        若不为空:继续判断购物车中是否有该商品
                                若有:
                                        取出count 将数量+1 
                                        将商品再次放入购物车中
                                若没有:
                                        将当前商品put进去 数量:1                                
        提示信息:你的xx已添加到购物车中

[code]/**
* 添加到购物车
*/
public class Add2CartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//0.设置编码
response.setContentType("text/html;charset=utf-8");
PrintWriter w = response.getWriter();

//1.获取商品的名称
String name=request.getParameter("name");
name=new String(name.getBytes("iso8859-1"),"utf-8");

//2.将商品添加到购物车
//2.1 从session中获取购物车
Map<String,Integer> map=(Map<String, Integer>) request.getSession().getAttribute("cart");

Integer count=null;

//2.2判断购物车是否为空
if(map==null){
//第一次购物  创建购物车
map=new HashMap<>();

//将购物车放入session中g
request.getSession().setAttribute("cart", map);

count=1;
}else{
//购物车不为空 继续判断购物车中是否有该商品
count = map.get(name);
if(count==null){//或者  map.content(name);  是否包含
//购物车中没有该商品——稍后在添加到购物车
count=1;
}else{
//购物车中有该商品
count++;
}
}
//将商品放入购物车中
map.put(name, count);

//3.提示信息
w.print("已经将<b>"+name+"</b>添加到购物车中<hr>");
w.print("<a href='"+request.getContextPath()+"/product_list.jsp'>继续购物</a>&nbsp;&nbsp;&nbsp;&nbsp;");
w.print("<a href='"+request.getContextPath()+"/cart.jsp'>查看购物车</a>&nbsp;&nbsp;&nbsp;&nbsp;");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
  • 3-2-3 点击购物车连接的时候 cart.jsp

        从session获取购物车
                判断购物车是否为空
                        若为空:提示信息
                        若不为空:遍历购物车即可

[code]<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="20%">
<tr>
<td>商品名称</td>
<td>商品数量</td>
</tr>
<%
//1.获取购物车
Map<String,Integer> map=(Map<String,Integer>)session.getAttribute("cart");

//2.判断购物车是否为空
if(map==null){
//2.1若为空 : 亲,购物车空空,先去逛逛~~  合并行
out.print("<tr><td colspan='2'>亲,购物车空空,先去逛逛~~</td></tr>");
}else{
//2.2若不为空 :遍历购物车
for(String name:map.keySet()){
out.print("<tr>");
out.print("<td>");
out.print(name);
out.print("</td>");
out.print("<td>");
out.print(map.get(name));
out.print("</td>");
out.print("</tr>");

}
}
%>
</table>

<hr>
<center>
<a href="/day1101/product_list.jsp">继续购物</a>&nbsp;&nbsp;&nbsp;
<a href="/day1101/clearCart">清空购物车</a>
</center>
</body>
</html>
  • 3-3 案例3-扩展清空购物车

        思路1:将购物车移除
        思路2:将session干掉

  • 3-3-1 步骤分析

        在cart.jsp上添加一个超链接 清空购物车
                <a href="/day1101/clearCart">清空购物车</a>
        在clearCart中需要调用session.invalidate()
        重定向到购物车页面

[code]/**
*清空购物车
*/
public class ClearCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.清空session
request.getSession().invalidate();

//2.重定向
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
  • 总结

  • 1 jsp:

        java 服务器页面
        就是在html代码嵌套java代码,
        本质上就是一个servlet,运行在服务器,接受请求,处理业务逻辑,生成动态内容
        jsp三个脚本:
                <%...%> java片段 
                        生成在jsp的service方法中
                <%=...%> 输出表达式
                        生成service方法中
                        不能";"结尾
                <%!...%> 声明成员

  • 2 cookie:浏览器端会话技术

        由服务器生成,key=value格式,通过响应头(set-cookie)写回浏览器
        保存在浏览器端,当浏览器下一次访问的时候,根据一定的规则携带不同的cookie,通过请求头(cookie)携带

        常用方法:

        构造:
                new Cookie(String key,String value);
        写回浏览器:
                response.addCookie(Cookie c)
        获取:
                Cookie[] request.getCookies();
        cookie的api:
                getName():获取cookie的名称
                getValue():获取cookie的值
                
                setMaxAge(int 秒):设置cookie在浏览器的存活时间
                        若设置为0:删除cookie(前提必须路径保持一致)
                setPath(String path):设置cookie的路径  /day1101/
                        若访问的路径中包含cookie的path,则携带过去
        注意:
                cookie不能跨浏览器
                cookie中不支持中文

  • 3 session:

        服务器端会话技术:
        依赖于cookie
                浏览器访问服务器,服务器获取jsessionid
                        若获取不到
                                创建一个sesion,将数据保存,将当前session的jsessionid通过cookie返回浏览器
                        若获取到
                                拿着该jsessionid去session池中查找有无该session
                                        若查找到:
                                                直接拿过来使用,将jsessionid写回浏览器
                                        若查找不到
                                                创建一个sesion,将数据保存,将当前session的jsessionid通过cookie返回浏览器
        
        常用方法:
                获取session:
                        HttpSession request.getSession():
                        
        域对象:
                session
                生命周期
                        创建:java代码中可以认为 第一次使用request.getSession创建
                        销毁:
                                服务器关闭
                                session超时
                                        默认超时
                                        手动设置超时
                                ★手动干掉session 
                                        session.invalidate()

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: