Session

简介: Session

什么是Session?

  • 服务器会给每个用户(浏览器)创建一个Session对象。
  • 一个Session独占一个浏览器,只要浏览器没有关闭,Session就一直存在。
  • 用户登陆后,整个网站都可以访问!(保存用户的信息到服务器)

重点方法

  • getId         获取Session的id
  • getServletContext        获取上下文对象
  • getAttribute          得到节点(多个Servlet之间数据访问数据)
  • setAttribute          设置节点(存数据),Session的setAttribute参数中,可以传入一个Object对象作为 节点对应的值。
  • removeAttribute        移除节点
  • invalidate                注销
  • isNew                   判断是不是新创建的Session

Session和Cookie的区别?

测试代码:

SessionDemo01:创建Session并设置节点信息。

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
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.setAttribute("name","李元芳");
        //向Session
        String id = session.getId();
        //判断是不是新创建的Session
        boolean isNew = session.isNew();
        if (isNew){
            resp.getWriter().write("session创建成功,ID="+id);   //会写到页面上
        }else {
            resp.getWriter().write("session创建失败,服务器中已存在,ID="+id);
        }
        //Session做了什么?
        Cookie cookie = new Cookie("JSESSIONID",id);
        resp.addCookie(cookie);
    }
}

SessionDemo02: SessionDemo01在创建Session后,SessionDemo02测试读取Session设置的节点数据。

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 SessionDemo02 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");
        //获取SessionDemo01的Session并读取节点 name 的数据
        HttpSession session = req.getSession();
        String name = (String) session.getAttribute("name");
        System.out.println(name);
    }
}

我们发现服务器给我们返回了一个关于 SessionId 的Cookie 作为响应。

相当于

        Cookie cookie = new Cookie("JSESSIONID",id);
        resp.addCookie(cookie);

再次访问

  • 除此之外,Session还可以存储 对象!!
  • 少用ServletContext存储信息(多了会爆炸),尽量用Session。
  • 只要一旦注销Session(session.invalidate()),会立即再生成一个新的Session

session.invalidate() 可以手动使 Session 失效,我们也可以在 web.xml 中设置自动注销Session:

<session-config>
<!-- 单位为分钟 设置session1分钟后自动过期 -->
<session-time-out>1</session-time-out>
</session-config>

Session和Cookie的区别:

  • Cookie 是把用户的数据写到用户的浏览器(可以保存多个)。
  • Session 是把用户的数据写到用户独占的Session中,由服务器保存(保存重要的信息)。
  • Session 对象由服务器创建。

Session使用场景:

  • 保存登陆用户的信息
  • 购物车信息
  • 整个网站中经常使用的数据
相关文章
|
1月前
|
存储 开发者
Session 使用详解
Session 使用详解
29 1
|
4月前
|
存储 NoSQL Java
什么是Cookie与Session之Session详解
什么是Cookie与Session之Session详解
39 0
|
6月前
|
存储 安全 NoSQL
97 # session
97 # session
18 0
|
10月前
|
存储 开发框架 安全
session
session
91 0
|
10月前
|
存储 应用服务中间件
Session浅谈
Session浅谈
48 0
|
11月前
|
存储 应用服务中间件 开发者
Session
Session
52 0
|
存储 SQL 开发框架
关于Session你需要了解这些
各种web框架早已考虑到这个问题,比如asp.net,是支持通过配置文件修改session的存储介质为sql server的,所有机器的会话数据都从同一个数据库读,就不会存在不一致的问题;
172 0
|
SQL Oracle 关系型数据库