Java Web 从入门到退坑 —— 第十二章 Session
By -gregPerlinLi-
1. 什么是 Session
1. Session 就是一个接口(HttpSession
)
2. Session 就是会话,它是用来维护客户端和服务器之间关联的一种技术。
3. 每个客户端都有自己的一个 Session 会话。
4. 在Session 会话中,我们经常用来保存用户登录的信息
2. 如何创建 Session 和获取(id
号,是否为新)
如何创建和获取 Session,其 API 都是一样的:
request.getSession();
第一次调用是创建 Session 会话,之后调用都是获取前面创建好的 Session 会话。
isNew()
判断倒是是不是刚创建出来的(新的)。
true
表示刚创建
false
表示获取之前创建
每个对话都有一个身份号码,也就是 ID 值,而且这个 ID 值是唯一的。
getId()
得到 Session 的唯一 ID 值。
示例代码:
protected void createOrGetSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Creating and getting session objects
HttpSession session = request.getSession();
// Judge whether the current session is newly created
boolean isNew = session.isNew();
// Gets the unique ID of the session
String id = session.getId();
response.getWriter().write("The ID of the session you get is: " + id + " <br/> ");
response.getWriter().write("Is this session newly created: " + isNew + " <br/> ");
}
3. Session 域数据的存取
示例代码:
/**
* Save data to session domain
*
* @param request request
* @param response response
* @throws ServletException e
* @throws IOException e
*/
protected void setAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().setAttribute("key1", "value1");
response.getWriter().write("Data has been saved to session");
}
/**
* Get the data in the session domain
*
* @param request request
* @param response response
* @throws ServletException e
* @throws IOException e
*/
protected void getAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object attribute = request.getSession().getAttribute("key1");
response.getWriter().write("The key1 data got from the session is: " + attribute);
}
4. Session 生命周期控制
public void setMaxInactiveInterval(int interval) {}
设置 Session 的超时时间,超过指定的时长(以秒为单位),Session 就会被销毁。
值为正数的时候,设定 Session 的超时时长。
值为负数的时候,设定 Session 永不超时(极少使用)。
public int getMaxInactiveInterval(int interval) {}
获取 Session 的超时时长。
示例代码:
protected void defaultLife(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Gets the default timeout of session
int maxInactiveInterval = request.getSession().getMaxInactiveInterval();
response.getWriter().write("The default timeout of session is: " + maxInactiveInterval + "s");
}
Session 默认的超时时长为 1800s
(30min
)
因为在 Tomcat 服务器的配置文件 web.xml
中,默认有以下的配置:
<session>
<session-timeout>30</session-timeout>
</session>
它表示配置了当前 Tomcat 服务器下所有的 Session 超时配置默认时长为 30min
。
如果希望自己的 Web 工程中默认的 Session 的超时时长为其他时长,你可以在自己的 web.xml
配置文件中做以上相同的配置,就可以修改 Web 工程中所有的 Session 的默认超时时长。
<!-- Indicates that all sessions created by the current web project have a 20 minute timeout by default -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
如果只想修改个别 Session 的超时时长,就可以使用上面的 API setMaxInactiveInterval(int interval)
来进行单独的设置
session.setMaxInactiveInterval(int interval)
单独设置超时时长
示例代码:
protected void life3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the session object first
HttpSession session = request.getSession();
// Set the current session timeout after 3 seconds
session.setMaxInactiveInterval(3);
response.getWriter().write("The current session has been set to timeout after 3 seconds!");
}
注意⚠️:Session 的超时指的是客户端两次请求的最大间隔时长!
public void invalidate() {}
让当前的 Session 对象立即超时(无效)
示例代码:
protected void deleteNow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the session object first
HttpSession session = request.getSession();
// Make the session session time out immediately
session.invalidate();
response.getWriter().write("Session has been set to timeout (invalid)");
}
5. 浏览器和 Session 之间关联的技术内幕
Session 技术,底层其实是基于 Cookie 技术来实现的,大致的原理如下图所示: