前言
本文分文两个demo进行,第一个demo主要是用来测试:什么时候创建HttpSession,第二个demo用来测试:什么时候销毁HttpSession。围绕这两个测试用例进行总结。
DEMO1
目录结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWeb_21</display-name>
<welcome-file-list>
<welcome-file>session.jsp</welcome-file>
</welcome-file-list>
</web-app>
session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" session="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>session.jsp</title>
</head>
<body>
<%
HttpSession session = request.getSession(false);
out.print(session);
%>
<br><br>
<a href="creatSession.jsp">creatSession.jsp</a>
</body>
</html>
creatSession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>creatSession.jsp</title>
</head>
<body>
<%=session%><br><br>
<a href="session.jsp">session.jsp</a>
</body>
</html>
初次访问session.jsp 返回null
访问creatSession后再访问session.jsp后,返回HttpSession
总结:什么时候创建HttpSession
1.是否游览器访问服务器的任何一个servlet或jsp,服务器都会立即创建一个HttpSession对象呢?
不一定。若当前的JSP(或Servlet)是客户端访问的当前WEB应用的第一个资源,且Jsp的Page指定的session属性为false,则服务器就不会为JSP创建一个HttpSession对象;
若当前JSP不是客户端访问的当前WEB应用的第一个资源,且其他页面已经创建一个HttpSession对象,则会返回和当前会话关联的HttpSession对象,不会创建一个新的HttpSession对象。
2.session="false"表示当前JSP页面禁用session隐含变量,但可以使用其他显式的HttpSession对象。
3.对于servlet而言:若servlet是客户端访问的第一个WEB应用资源。则只有调用了request.getSession()或request.getSession(true)才会创建HttpSession对象。
4.在servlet中如何获取HttpSession对象
HttpServletRequest
HttpSession | getSession() Returns the current session associated with this request, or if the request does not have a session, creates one. |
HttpSession | getSession(boolean create) Returns the current HttpSession associated with this request or, if there is no current session andcreate is true, returns a new session. |
getSession(boolean create):若create为false,则若没有和当前JSP页面关联的HttpSession对象则返回null,若有则返回
该HttpSession.
若create为true,一定返回一个HttpSession对象,若没有和当前JSP页面关联的HttpSession对象,则服务器创建一个新的HttpSession对象返回,若有直接返回关联的HttpSession对象。
request.getSession()等同于request.getSession(true).
DEMO2
在createSession界面一直刷新后一直是同一个Session
修改creatSession.jsp如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>creatSession.jsp</title>
</head>
<body>
<%=session%>
<%session.invalidate(); %>
<br><br>
<a href="session.jsp">session.jsp</a>
</body>
</html>
此后每次刷新后都会不是同一个session
若改成如下
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>creatSession.jsp</title>
</head>
<body>
<%=session%>
<%session.setMaxInactiveInterval(5); %>
<br><br>
<a href="session.jsp">session.jsp</a>
</body>
</html>
则五秒后才会新建一个session
总结:什么时候销毁HttpSession
1.直接调用HttpSession的invalidate()方法,该方法使HttpSession失效
2.服务器卸载了当前web应用
3.超出HttpSession的过期时间:
①session.setMaxInactiveInterval(5)单位为秒;
②在tomcat/conf/web.xml中设置,单位为分钟;
③在应用/web-inf/web.xml中设置,单位为分钟。
4.并不是关闭了游览器就销毁了session,可通过持久化cookie或url重写访问session。