request.getSession()第一次调用是:创建 Session 会话之后调用都是:获取前面创建好的 Session 会话对象。创建和获取 Session 的 API 是一样的isNew(); 判断到底是不是刚创建出来的(新的)true 表示刚创建false 表示获取之前创建每个会话都有一个身份证号。也就是 ID 值。而且这个 ID 是唯一的。getId() 得到 Session 的会话 id 值。
- html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="pragma" content="no-cache"/>
- <meta http-equiv="cache-control" content="no-cache"/>
- <meta http-equiv="Expires" content="0"/>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Sessiontitle>
- <base href="http://localhost:8080/14_session/"/>
- <style type="text/css">
- ul li {
- list-style: none;
- }
- style>
- head>
- <body>
- <iframe name="target" width="500" height="500" style="float: left;">iframe>
- <div style="float: left;">
- <ul>
- <li><a href="sessionServlet?action=createSession" target="target">Session的创建和获取(id号、是否为新创建)a>li>
- <li><a href="sessionServlet?action=setAttribute" target="target">Session域数据的存储a>li>
- <li><a href="sessionServlet?action=getAttribute" target="target">Session域数据的获取a>li>
- <li>Session的存活li>
- <li>
- <ul>
- <li><a href="" target="target">Session的默认超时及配置a>li>
- <li><a href="" target="target">Session3秒超时销毁a>li>
- <li><a href="" target="target">Session马上销毁a>li>
- ul>
- li>
- <li><a href="" target="target">浏览器和Session绑定的原理a>li>
- ul>
- div>
- body>
- html>
BaseServlet:
- public abstract class BaseServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doPost(req, resp);
- }
-
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 解决post请求中文乱码问题
- // 一定要在获取请求参数之前调用才有效
- request.setCharacterEncoding("UTF-8");
- response.setContentType("text/html;charset=UTF-8");
-
- String action = request.getParameter("action");
- // 获取 action 业务鉴别字符串,获取相应的业务 方法反射对象
- try {
- Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
- // 调用目标业务 方法
- method.invoke(this, request, response);
- } catch (Exception e) {
- // e.printStackTrace();
- }
- }
- }
SessionServlet程序:
- @WebServlet(name = "SessionServlet", value = "/sessionServlet")
- public class SessionServlet extends BaseServlet {
- /**
- * 创建Session对象,并查看其是否为第一次创建及其id值
- *
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- protected void createSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 1. 获取 Session 对象
- HttpSession session = request.getSession();
-
- // 2. 查看创建的 Session 对象是否为新
- boolean aNew = session.isNew();
-
- // 3.查看创建的 Session 对象的ID值
- String id = session.getId();
-
- response.getWriter().write("已创建了 Session 对象
"); - response.getWriter().write("查看创建的 Session 对象是否为新:" + aNew + "
"); - response.getWriter().write("查看创建的 Session 对象的ID值:" + id + "
"); - }
-
- /**
- * 往Session中保存数据
- *
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- protected void setAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 1. 获取 Session 对象
- HttpSession session = request.getSession();
- // 2.向 Session 中保存数据
- session.setAttribute("key", "value");
-
- response.getWriter().write("已经往 Session 中保存了数据
"); -
- }
-
- /**
- * 获取Session中的数据
- *
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- protected void getAttribute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 1. 获取 Session 对象
- HttpSession session = request.getSession();
- // 2. 获取 Session 域中的数据
- Object key = session.getAttribute("key");
-
- response.getWriter().write("获取 Session 中的数据为:" + key + "
"); -
- }
-
-
- }
public void setMaxInactiveInterval(int interval)设置 Session 的超时时间(以秒为单位),超过指定的时长,Session就会被销毁。值为正数的时候,设定 Session 的超时时长。值为负数表示永不超时(极少使用)public int getMaxInactiveInterval()获取 Session 的超时时间public void invalidate()让当前 Session 会话马上超时无效。
30
代码示例:
- protected void defaultLift(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 1. 获取默认超时时长 Session 对象
- HttpSession session = request.getSession();
- // 2. Session 对象的超时时长
- int maxInactiveInterval = session.getMaxInactiveInterval();
-
- response.getWriter().write("获取 Session 中的默认超时时长为:" + maxInactiveInterval + "
"); -
- }
结果:

-
- <session-config>
- <session-timeout>20session-timeout>
- session-config>
再次运行Servlet程序,得到结果:

- protected void lift3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 1. 获取 Session 对象
- HttpSession session = request.getSession();
-
- // 2. 设置 Session 对象的超时时长为3秒
- session.setMaxInactiveInterval(3);
-
- // 3.获取 Session 对象的超时时长
- int maxInactiveInterval = session.getMaxInactiveInterval();
-
- response.getWriter().write("设置 Session 中的超时时长成功
"); - response.getWriter().write("获取 Session 中的超时时长为:" + maxInactiveInterval + "
"); -
- }
代码示例:
- protected void deleteLift(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 1. 获取 Session 对象
- HttpSession session = request.getSession();
-
- // 2. 让 Session 会话马上销毁
- session.invalidate();
-
- response.getWriter().write("Session 已经设置为超时(无效)");
-
- }