2

对Session运用的实战与原理剖析详解

 1 year ago
source link: https://blog.51cto.com/u_15568258/5638876
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Table of Contents

一.Session基础

1.1什么是Session?

Session是基于Web服务器的状态保持的一种方法。

  • 服务器将为每个用户(浏览器)创建一个Session对象。
  • 一个Session独占浏览器。只要浏览器未关闭(Tomcat未打开或JSSessionID未清除)(关闭网页不影响),Session就存在。
  • 用户登录后,可以访问整个网站!–>保存用户信息。

1.2 Session和的区别?

Cookie原理图如下:

对Session运用的实战与原理剖析详解_服务器
  • Cookie把用户的数据给到用户的游览器,只有游览器本地保存(它可以保持多个)

  • Session把数据写个服务器端的用户对应的Session中,每个用户独占一个。(保存重要的信息,避免浪费服务器的资源)

    Session原理图如下:

    对Session运用的实战与原理剖析详解_服务器_02

二.Session保持会话实战

1.我们可以在之前cookie项目过程下,新建类名为:SessionDemo01的工程类。

2.继承HttpServlet,重写doGet,和doPost,并且将doPost直接调用doGet。

3.在doGet重写方法中,先设置编码格式,防止中文乱码出现。

req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");

4.得到Session:

 HttpSession session = req.getSession();

5.给Session里存入东西。

session.setAttribute("name","菜猪");

6.获取到Session的id值:

String id = session.getId();

7.写一个判断,让Session存在于Session不存在分别输出俩种不同的提示文字:

if(session.isNew()){
    resp.getWriter().write("session创建成功"+id);
}else {
    resp.getWriter().write("session已经存在了"+id);
}

8.给上面新建的Session类注册一个url,以及写一下映射:

<servlet>
    <servlet-name>session01</servlet-name>
    <servlet-class>com.gowork.servlet.SessionDemo1</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>session01</servlet-name>
    <url-pattern>/se1</url-pattern>
</servlet-mapping>

9.启动服务器,打开游览器,查看结果如下:

我使用了一个没有访问过的某狐游览器,第一次访问得到如下截图:

对Session运用的实战与原理剖析详解_java_03

之后刷新,打开新的标签页以及关闭游览器再次打开,都是出现这个页面:

对Session运用的实战与原理剖析详解_f5_04

我们换一个游览器,出现下面页面,我们对照来看;

对Session运用的实战与原理剖析详解_服务器_05

我们看上图,发现,俩个游览器同时访问该url,得到的Session的id不一样,这恰恰的验证了最开始的Session的特性:

  • 服务器将为每个用户(浏览器)创建一个Session对象。
  • 一个Session独占浏览器。只要浏览器未关闭(Tomcat未打开或JSSessionID未清除)(关闭网页不影响),Session就存在。

我们在当前页面中,按f12,发现:我们写的是Session,为什么会出现Cookie呢?

对Session运用的实战与原理剖析详解_服务器_06

我们来看一下Session创建的时候,做了一个什么事情。

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

在Session创建的时候相当于创建了应该Cookie对象,把Session的id值放到了里面。

2.1 Session跨servlet使用

1.新建一个类,类名为SessionDemo2。

2.和上面一样,先继承HttpServle,t重写doGet,和doPost,并且将doPost直接调用doGet。

3.获取Session,设置字符编码:

req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();

4.获取Session里name的值,并且输出到控制台:

String naem = (String) session.getAttribute("name");
System.out.println(naem);

5.启动服务器,游览器先访问se1,然后再访问se2:

对Session运用的实战与原理剖析详解_服务器_07

上图,我们可以看出,在类SessionDemo2可以获取到SessionDemo1中session里key名为name的值,并且输出。

2.2 Session存储多种类型

1.我们新建一个person类:

private String name;
private int age;

2.然后有参构造函数:

我们使用快捷键

有参构造方法:alt+ins选择Constructor
无参构造方法:alt+ins选择Constructor然后选择下方的SelectNone

public person(String name, int age) {
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

public int getAge() {
    return age;
}


public void setName(String name) {
    this.name = name;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public String toString() {
    return "person{" +
        "name='" + name + '\'' +
        ", age=" + age +
        '}';
}

3.我们在SessionDomo1里new一个person对象。

session.setAttribute("name",new person("caizhu",1));

4.在SessionDomo2输出person对象:

person naem = (person) session.getAttribute("name");
System.out.println(naem.toString());

5.结果检验:

对Session运用的实战与原理剖析详解_服务器_08

2.3 注销Session

创建SessionDomo3。

注销Session只需要一句代码,如下:

HttpSession session = req.getSession();
session.removeAttribute("name");
session.invalidate();
对Session运用的实战与原理剖析详解_java_09

当前ID为:D142028FFFD305DBEE37C8FF22565EAA

访问se3,执行删除Session。再次访问Se2,因为Session被删除,se2会空指针报错:

对Session运用的实战与原理剖析详解_java_10

再次访问se1:

对Session运用的实战与原理剖析详解_服务器_11

现在的ID变为:D11FAC1C9F215D26E53FEB3FBB9D0826

值已经改变了,说明之前对Session的删除是有效的。

2.3.1设置Session的失效时间

这里的session-timeout里的数字的单位是分钟。下文的代码代表着,15分钟之后Session失效。

<seeson-config>
    <session-timeout>15</session-timeout>
</seeson-config>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK