b站狂胜笔记3-javaWeb-Session

目录

7.Cookie,Session

7.1会话

7.2保存会话的两种技术

session

7.3cookie

删除cookie

7.4Session(重点) 


7.Cookie,Session

7.1会话

会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为有状态会话:一个同学来过教室,下次在来教室,我们会知道这个同学曾经来过称之为有状态会话

你怎么证明你是学校目前的学生?

目前有两个对象:你 学校

  1. 学费发票        学校给你学费发票
  2. 学校登记        学校标记你过了

一个网站怎么证明你来过?

两个对象:客户端 服务端

  1. 服务端给客户端一个信件,客户端下次访问带上信件就可以了,cookie
  2. 服务器登记你来过了,下次来的时候则直接匹配.Session

7.2保存会话的两种技术

  • 客户端技术(响应与请求)

session

  • 服务器技术,利用这个技术可以保存用户的会话信息,我们可以把信息或者数据放在Session中!

常见常见:网站登录后,下次不用在登录了,第二次访问直接就上去了!

7.3cookie

  1. 从请求中拿到cokie信息
  2. 服务器响应给客户端cokie
Cookie[] cookies = req.getCookies();//获得Cookie
cookie.getName();//获取cookie的k(名字)
cookie.getValue();//获取cookies的v(值)
Cookie cookie = new Cookie("Time",System.currentTimeMillis()+"");//新建一个cookie
cookie.setMaxAge(60*60*24);//设置cookies的有效时间

 cookie:一般会保存在电脑本地的用户目录AppData下

一个网站cookie是否存在上线? 聊聊细节问题

  • 一个cookie只能保存一个信息
  • 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
  • cookie大小有限制为4kb
  • 300个cookie浏览器的上线

删除cookie

  • 不设置有效期,关闭浏览器,自动失效
  • 将cookie的有效期设置为0

7.4Session(重点) 

什么是Session:

  • 服务器会给每一个用户(浏览器)创建一个Session对象。
  • 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在。
  • 用户登录之后整个网站它都可以访问!保存用户信息-->保存购物车信息

Sesion与Cookie的区别:

  • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
  • Sesion是把用户的数据写到用户独占的Sesion中,服务器端保存(保存重要信息 ,减少服务器资源浪费)
  • Sesion对象由服务器创建

使用场景:

  • 保存一个登录用户信息;
  • 购物车信息;
  • 在整个网站中经常会使用的数据将它保存在Session中 

使用Session:

package com.tang.Servlet.Tang;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        //得到Session
        HttpSession session = req.getSession();
        //给Session中存东西
        session.setAttribute("name", new User(1,15,"小步"));
        //获取Session的id
        String id = session.getId();

        //判断Session是不是新创建的
        if (session.isNew()){
            resp.getWriter().write("Session创建成功,id为:"+id);
        }else {
            resp.getWriter().write("Session已经在服务器存在了,id为:"+id);
        }
        //Session在创建的时候做了什么事情?
//        Cookie cookie = new Cookie("JSESSIONID",req.getSession().getId());
//        resp.addCookie(cookie);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
package com.tang.Servlet.Tang;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

public class SessionDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //得到Session
        req.setCharacterEncoding("GB2312");
        resp.setCharacterEncoding("GB2312");
        HttpSession session = req.getSession();
        User name = (User) session.getAttribute("name");
//        String decode = URLDecoder.decode(name, "UTF-8");
//        System.out.println(decode);
        resp.getWriter().write(name.toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
package com.tang.Servlet.Tang;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SessionDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("name");
        session.invalidate();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

XML配置会话自动过期:

<!-- 设置Session的失效时间 -->
<session-config>
   <!-- 1分钟后Session自动失效,以分钟为单位 -->
   <session-timeout>1</session-timeout>
</session-config>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

123小步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值