狂神说Java Web学习笔记_Session
原理图
服务器会给每一个用户(浏览器)创建一个session对象
一个session独占一个浏览器,主要浏览器没有关,这个session就存在
登录之后,整个网站都可以访问
常用场景
保存一个用户的登录信息
在整个网站中经常会使用到的数据
常用的session方法
//得到Session
HttpSession session = req.getSession();
//给Session中存数据
session.setAttribute("name","admin");
//获取Session中的数据
session.getAttribute("name");
//删除Session中的数据
session.removeAttribute("name");
//获取Session的ID
String sessionId = session.getId()
//手动注销Session
Sessionsession.invalidate();
Sessiondemo03
手动注销session invalidate
另一种方式,web.xml设置默认的注销时间
Session-timeout
cookie设置超时
应用
使用Session存储字符串
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 SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
String id = session.getId();
//给session中存入字符串
session.setAttribute("name", "闹闹");
String name = (String) session.getAttribute("name");
resp.getWriter().write("获取到的sessionId为:" + id + ", 获取到的name属性的值为:" + name);
}
}
<servlet>
<servlet-name>sessionDemo01</servlet-name>
<servlet-class>com.dawn.session.SessionDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo01</servlet-name>
<url-pattern>/sessionDemo01</url-pattern>
</servlet-mapping>
不同浏览器获取到的SessionId值不一样
使用Session存储对象
import com.dawn.entity.Person;
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 IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
String id = session.getId();
//给session中存入对象
Person person = new Person("bambi", 26);
session.setAttribute("person", person);
Person personGet = (Person) session.getAttribute("person");
resp.getWriter().write("获取到的sessionId为:" + id + ", 获取到的person对象的内容为:" + personGet.toString());
}
}
<servlet>
<servlet-name>sessionDemo02</servlet-name>
<servlet-class>com.dawn.session.SessionDemo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo02</servlet-name>
<url-pattern>/sessionDemo02</url-pattern>
</servlet-mapping>
手动注销Session
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 IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
HttpSession session = req.getSession();
session.invalidate();
resp.getWriter().write("设置session失效成功");
}
}
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 SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
String id = session.getId();
if (session.isNew()) {
resp.getWriter().write("session创建成功,id为: " + id);
} else {
resp.getWriter().write("session在服务器中已经存在,无需再次创建,对应id为: " + id);
}
}
}
<servlet>
<servlet-name>sessionDemo03</servlet-name>
<servlet-class>com.dawn.session.SessionDemo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionDemo03</servlet-name>
<url-pattern>/sessionDemo03</url-pattern>
</servlet-mapping>
调用注销session的servlet接口之前,可以获取到session
紧接着调用注销session接口
再次访问获取session接口,发现已经重新生成了一个新的session,之前的那个已经被注销了
设置Session自动过期
<!--设置Session默认的失效时间-->
<session-config>
<!--1分钟后Session自动失效,以分钟为单位-->
<session-timeout>1</session-timeout>
</session-config>
访问sessionDemo01接口
预期1分钟之后再次访问这个接口的时候,应该重新生成一个新的sessionId