介绍
HTTP协议是一个无状态协议。也就是说当一个浏览器客户程序与服务器之间多次进行HTTP请求/响应时,HTTP协议本身没有提供服务器连接跟踪特定浏览器端状态的规范。因此我们需要采取一些手段来记录浏览器客户端的状态。
Web服务器跟踪客户状态的四种方法:
1.在HTML表单中加入隐藏字段,它包含用于跟踪客户状态的数据。
2.重写URL,使它包含用于跟踪客户状态的数据。
3.用Cookie来传送用于跟踪客户状态的数据。
4.使用会话(session)机制。
HttpSession的生命周期及会话范围
会话范围是指浏览器与Web应用进行一次会话的过程。(浏览器不关闭)会话范围与Session的生命周期对应。因此Web组件只要共享一个HttpSession就可以在一次会话范围内共享数据。
什么时候开始一个新的会话
开启新的会话是指,在Servlet容器中创建一个新的HttpSession对象
1.第一次访问一个Web应用中支持Session的网页。
2.Web应用的一次会话被销毁后,浏览器再次访问Web应用中支持Session的网页。
什么时候会话会结束
会话结束即Servlet容器中HttpSession被释放。
1.浏览器进程终止。
2.服务器端执行invalidate()方法
3.会话过期。
会话过期
会话处于不活跃状态(客户端不与Web端支持会话的页面交互)的时间过久,会话就会结束。setMaxInactiveInterval(int interval)方法是用来设置失效时间得(单位秒)。如果为负数,则表示永远不过期。
示例
可以通过HttpServletRequest接口提供了两个与会话有关的方法。
1.getSession():使得当前HttpServlet支持会话。假如存在HttpSession,则返回。若不存在,则创建一个,并返回。
2.getSession(boolean create)方法:如果参数create为true则等价于getSession()方法。若为false,若存在HttpSession则返回。若不存在则返回null.
@WebServlet("/endServlet")
public class EndServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("GBK");
resp.setContentType("text/html;charset=gbk");
HttpSession session = req.getSession(false);
if(session!=null){
//结束会话
session.invalidate();
}
PrintWriter writer = resp.getWriter();
writer.write("已经注销");
getServletContext().getRequestDispatcher("/index.html").include(req,resp);
}
}
@WebServlet("/test_session")
public class TestSessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
resp.setContentType("text/html;charset=gbk");
String username;
PrintWriter writer = resp.getWriter();
//使Servlet支持session
HttpSession session = req.getSession();
//等价于req.getSession(true);
//如果Session是新创建
if(session.isNew()){
writer.write("请登录");
//包含
req.getRequestDispatcher("login.html").include(req,resp);
return;
}else {
username = req.getParameter("username");
if(username!=null&&username!=""){
//会话范围保存属性
session.setAttribute("username",username);
}else {
username = (String) session.getAttribute("username");
}
}
writer.write("欢迎"+username);
getServletContext().getRequestDispatcher("/end.html").forward(req,resp);
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<form action="endServlet" method="post">
<input value="注销" type="submit">
</form>
</html>
<html>
<head>开始</head>
<a href="test_session">进入网站</a>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Title</title>
</head>
<body>
<form action="/app/test_session" method="post" >
<input name="username" type="text">
<input value="提交" type="submit" >
</form>
</body>
</html>
标准会话管理器 Standar dManager
Standar dManager是默认的标准会话管理。当TomCat服务器终止或者单个Web应用被终止时,会对被终止的Web应用的HttpSession对象进行持久化,保存到本地。下次打开Web应用的时候自动加载。
持久化会话管理器 PersistentManager
PersistentManager把存放HttpSession对象的永久性存放设备称为Store。
PersistentManager具有以下功能
1.当Tomcat服务器关闭或重启,或者单个Web应用被重启时,回堆Web应用的HttpSession对象进行持久化,把他们保存到会话Store中。
2。具有容错功能,及时把HttpSession对象备份到会话Store中。当Tomcat服务器意外关闭后再重启时,可以从会话Store中回复HttpSession对象。
3.可以灵活控制在内存中的HttpSession对象的数目,将部分HttpSession对象转移到会话Store中。
Store有两个实现类
FileStore
将HttpSession对象保存到文件中
配置方法:在context.xml文件中加入
Manger元素的属性
JDBCStore
JDBCStore将HttpSession对象保存在一张表中。这张表的字段有
context.xml
为了确保TomCat能够访问MySql应该将JDBC驱动器放到WEB-INF/lib目录下
会话监听器
其中HttpSessionLisstener和HttpSessionAttributeListener必须在Web.xml文件中通过listener元素配置另外两个则只需要让响应类实现他们即可。