类似于去服务器注册账号,只要服务器不停机,自己注册的账号一直会存在服务器。
什么是Session:
1.服务器会给每一个用户(浏览器)创建一个对象;
2.一个Session独占一个浏览器,只要浏览器没有关闭,这个session就会存在;
3.用户登录之后,整个网站它都可以访问!--->保存用户的信息;
//session在创建的时候,做了什么事情? // Cookie cookie = new Cookie("JSESSIONID", id); // resp.addCookie(cookie);
其实
session服务器端
cookie客户端
Session可以存对象
存放对象
package com.kuang.servlet;
import com.kuang.pojo.Persion;
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.setHeader("Content-type","text/html;charset=utf-8");
//得到session对象
HttpSession session = req.getSession();
//给Session中存东西
session.setAttribute("name",new Persion("鸡哥",3));
//获取Session的ID
String id = session.getId();
System.out.println("您的sessionID为:"+id);
//判断Session是不是新创建的
if (session.isNew()){
resp.getWriter().write("session创建成功,sessionID为:"+id);
}else {
resp.getWriter().write("session已经在服务器中存在了,sessionID为:"+id);
}
//session在创建的时候,做了什么事情?
// Cookie cookie = new Cookie("JSESSIONID", id);
// resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
取对象
package com.kuang.servlet;
import com.kuang.pojo.Persion;
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.setHeader("Content-type","text/html;charset=utf-8");
//得到session对象
HttpSession session = req.getSession();
//获得session的name属性
Persion persion = (Persion)session.getAttribute("name");
System.out.println(persion.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
实体类
package com.kuang.pojo;
import java.util.Objects;
public class Persion {
String name;
int age;
public Persion(String name, int age) {
this.name = name;
this.age = age;
}
public Persion() {
}
@Override
public String toString() {
return "Persion{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
终于理解到为什么每次s1是session已经存在了,因为每次项目启动连接到服务端8080页面就创建了session,你在去访问是s1自然是已存在面
注销Session
我这里面注销了之后没有报空指针,只是取person取出来个null
报错的是System.out.println(name);空指针,但是再注销的时候,访问S2会立即生成新的Session
package com.kuang.servlet;
import com.kuang.pojo.Persion;
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.setHeader("Content-type","text/html;charset=utf-8");
//得到session对象
HttpSession session = req.getSession();
//给Session中存东西
session.setAttribute("name",new Persion("鸡哥",3));
//获取Session的ID
String id = session.getId();
System.out.println("您的sessionID为:"+id);
//判断Session是不是新创建的
if (session.isNew()){
resp.getWriter().write("session创建成功,sessionID为:"+id);
}else {
resp.getWriter().write("session已经在服务器中存在了,sessionID为:"+id);
}
//session在创建的时候,做了什么事情?
// Cookie cookie = new Cookie("JSESSIONID", id);
// resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.kuang.servlet;
import com.kuang.pojo.Persion;
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.setHeader("Content-type","text/html;charset=utf-8");
//得到session对象
HttpSession session = req.getSession();
//获得session的name属性
Persion persion = (Persion)session.getAttribute("name");
resp.getWriter().write(session.getId());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注销
package com.kuang.servlet;
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;
//注销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("name");
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Session 和 Cookie的区别
1.Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
2.Session 把用户的数据写到用户独占Session中,服务器端保存(保存重要的信息,减少服务器的浪费)
3.Session对象由服务器创建!
服务器在产生一个cookie,cookie的name是sessionid,值sessionid
自我理解
Session:就是服务器存东西,想宝库一样,它会发放给每一个用户一个Sessionid(使用cookie发放给客户端,也就是存放在浏览器上),这就相当于一把钥匙,用户可以通过它取得宝库里的所有属于它的东西。
cookie:就是来做客,给你发的小饼干,这个饼干的东西,就是服务器给的一个数据,这个数据只能存字符串,(而session可以存对象),表示你来过,下次请求服务器的时候,带着cookie来,服务器就知道你上一次来过了!
servletContext是所有用户共用一个,session是每个人一个
中间会有一层中间商,这个的话是个悬念,后面会换一个名字出现ApplicationContext