1、Cookie、Session

1.1、会话

会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会话;
你能怎么证明你是旅商的学生?
你

  1. 发票 西开给你发票
  2. 学校登记 旅商标记你来过了

一个网站,怎么证明你来过?
客户端 服务端

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

    1.2、保存会话的两种技术

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

session

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

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

1.3、Cookie

2.Cookie、Session - 图1

  1. 从请求中拿到cookie信息
  2. 服务器响应给客户端cookie

Cookie[] cookies = req.getCookies(); //获得Cookie
cookie.getName(); //获得cookie中的key
cookie.getValue(); //获得cookie中的vlaue
new Cookie(“lastLoginTime”, System.currentTimeMillis()+””); //新建一个cookie
cookie.setMaxAge(246060); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie
cookie:一般会保存在本地的 用户目录下 appdata;
一个网站cookie是否存在上限!聊聊细节问题

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

删除Cookie;

  • 不设置有效期,关闭浏览器,自动失效;
  • 设置有效期时间为 0 ;

编码解码:
URLEncoder.encode(“金融啊”,”utf-8”)
URLDecoder.decode(cookie.getValue(),”UTF-8”)

1.4、Session(重点)

2.Cookie、Session - 图2
什么是session:服务器给每个用户(浏览器)创建一个 session对象;
一个session独占一个浏览器,只要浏览器没有关闭,这个session就存在;
用户登录之后,整个网站都可以访问!—>保存用户信息;保存购物车的功能…
使用session

  1. public class Person {
  2. String name;
  3. String gender;
  4. public Person(String name, String gender) {
  5. this.name = name;
  6. this.gender = gender;
  7. }
  8. public Person() {
  9. }
  10. @Override
  11. public String toString() {
  12. return "Person{" +
  13. "name='" + name + '\'' +
  14. ", gender='" + gender + '\'' +
  15. '}';
  16. }
  17. }

**

  1. public class SessionDemo extends HttpServlet {
  2. @Override
  3. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  4. //解决乱码的问题
  5. req.setCharacterEncoding("UTF-8");
  6. resp.setCharacterEncoding("UTF-8");
  7. //浏览器支持的格式
  8. resp.setContentType("text/html;charset=utf-8");
  9. //得到session
  10. HttpSession session = req.getSession();
  11. //往serssion放东西
  12. session.setAttribute("name","nicai");
  13. //往serssion放东西
  14. session.setAttribute("person",new Person("金融啊","男"));
  15. //获取session的id
  16. String sessionId = session.getId();
  17. //判断session是否创建
  18. if (session.isNew()){
  19. resp.getWriter().print("创建session它的id是:"+sessionId);
  20. }else {
  21. resp.getWriter().print("session的id是:"+sessionId);
  22. }
  23. //Session创建的时候做了什么事情;
  24. // Cookie cookie = new Cookie("JSESSIONID",sessionId);
  25. // resp.addCookie(cookie);
  26. }
  27. @Override
  28. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  29. doGet(req, resp);
  30. }
  31. }


获取Session的person

  1. public class SessionDemo02 extends HttpServlet {
  2. @Override
  3. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  4. //解决乱码的问题
  5. req.setCharacterEncoding("UTF-8");
  6. resp.setCharacterEncoding("UTF-8");
  7. //浏览器支持的格式
  8. resp.setContentType("text/html;charset=utf-8");
  9. //得到session
  10. HttpSession session = req.getSession();
  11. //获取name
  12. Person person = (Person) session.getAttribute("person");
  13. System.out.println(person.toString());
  14. }
  15. @Override
  16. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  17. doGet(req, resp);
  18. }
  19. }


摧毁session: 手动注销Session


public class SessionDemo03 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.removeAttribute("person");
            //手动注销Session
            session.invalidate();
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doGet(req, resp);
    }
}


设置Session的默认失效时间
Session会自动的过期:web.xml配置

<!--    设置session默认的失效世间-->
    <session-config>
<!--        1分钟失效以分钟为单位-->
        <session-timeout>1</session-timeout>
    </session-config>