zoukankan      html  css  js  c++  java
  • HttpSession

    1.Session机制

    session机制采用的是在服务器端保持 HTTP 状态信息的方案 。服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。

    当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否包含了一个session标识(即sessionId),

    如果已经包含一个sessionId则说明以前已经为此客户创建过session,服务器就按照session id把这个session检索出来使用

    (如果检索不到,可能会新建一个,这种情况可能出现在服务端已经删除了该用户对应的session对象,但用户人为地在请求的URL后面附加上一个JSESSION的参数)。

    如果客户请求不包含sessionId,则为此客户创建一个session并且生成一个与此session相关联的sessionId,这个session id将在本次响应中返回给客户端保存。

    2.HttpSession 的生命周期:

    1).什么时候创建 HttpSession 对象

        是否浏览器访问服务端的任何一个JSP或 Servlet,服务器都会立即创建一个 HttpSession 对象呢?不一定。

           >若当前的 JSP(或 Servlet) 是客户端访问的当前 WEB 应用的第一个资源,

              且 JSP 的 page 指定的 session 属性值为 false,

              则服务器就不会为 JSP 创建一个 HttpSession 对象;

           >若当前 JSP 不是客户端访问的当前 WEB 应用的第一个资源,且其他页面已创建一个 HttpSession 对象

             则当前 JSP 页面会返回一个会话的 HttpSession 对象,而不会创建一个新的 HttpSession‘ 对象

    session=“false“ 到底表示什么意思?当前 JSP 页面禁用 session 隐含变量!但可以使用其他的显式的 HttpSession 对象

    对于 Serlvet 而言:若 Serlvet 是客户端访问的第一个 WEB 应用的资源,则只有调用了 request.getSession() 或 request.getSession(true) 才会创建 HttpSession 对象

    2). Session 对象的销毁:

    ①. 直接调用 HttpSession 的 invalidate()

    ②. HttpSession 超过过期时间.

          > 返回最大时效: getMaxInactiveInterval() 单位是秒

          > 设置最大时效: setMaxInactiveInterval(int interval)

    > 可以在 web.xml 文件中配置 Session 的最大时效, 单位是分钟. 

    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>

    ③. 卸载当前 WEB 应用. 

    注意: 关闭浏览器不会销毁 Session!

    3.两个浏览器窗口访问应用程序会使用同一个session 

    通常session cookie是不能跨窗口使用的(IE 8 版本以前),当你新开了一个浏览器窗口进入相同页面时,系统会赋予你一个新的session id,这样信息共享的目的就达不到了。
    此时可以先把session id保存在persistent cookie中(通过设置cookie的最大有效时间),

    然后在新窗口中读出来,就可以得到上一个窗口的session id了,这样通过session cookie和persistent cookie的结合就可以实现了跨窗口的会话跟踪

    4.HttpSession接口中的方法 

    getId方法

    getCreationTime方法

    getLastAccessedTime方法

    setMaxInactiveInterval方法

    getMaxInactiveInterval方法

    isNew方法

    如果客户端请求消息中返回了一个与Servlet程序当前获得的HttpSession对象的会话标识号相同的会话标识号,则认为这个HttpSession对象不是新建的。

    invalidate方法

    getServletContext方法

    setAttribute方法

    getAttribute方法

    removeAttribute方法

    getAttributeNames方法

    HttpServletRequest接口中的Session方法 

    getSession方法

    public HttpSession getSession(boolean create)

    public HttpSession getSession()

    isRequestedSessionIdValid方法

    isRequestedSessionIdFromCookie方法

    isRequestedSessionIdFromURL方法

    5.利用URL重写实现Session跟踪 

    Servlet规范中引入了一种补充的会话管理机制,它允许不支持Cookie的浏览器也可以与WEB服务器保持连续的会话。

    这种补充机制要求在响应消息的实体内容中必须包含下一次请求的超链接,并将会话标识号作为超链接的URL地址的一个特殊参数。
    将会话标识号以参数形式附加在超链接的URL地址后面的技术称为URL重写。

    如果在浏览器不支持Cookie或者关闭了Cookie功能的情况下,WEB服务器还要能够与浏览器实现有状态的会话,

    就必须对所有可能被客户端访问的请求路径(包括超链接、form表单的action属性设置和重定向的URL)进行URL重写。

    HttpServletResponse接口中定义了两个用于完成URL重写方法:

    encodeURL方法

    encodeRedirectURL方法

    练习:登录与注销

    hello.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>Insert title here</title>
    </head>
    <body>
    
        SessionID: <%= session.getId() %>
        <br><br>
        
        IsNew: <%= session.isNew() %>
        <br><br>
        
        MaxInactiveInterval: <%= session.getMaxInactiveInterval() %>
        <br><br>
        
        CreateTime: <%= session.getCreationTime() %>
        <br><br>
    
        LastAccessTime: <%= session.getLastAccessedTime() %>
        <br><br>
        
        Hello: <%= request.getParameter("username") %>
        <br><br>
        
        <% 
            session.setAttribute("username", request.getParameter("username")); 
        %>
        
        <a href="<%= response.encodeURL("login.jsp") %>">重新登录</a>    
        &nbsp;&nbsp;&nbsp;&nbsp;
        <a href="<%= response.encodeURL("logout.jsp") %>">注销</a>    
        
    </body>
    </html>

    login.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>Insert title here</title>
    </head>
    <body>
    
        SessionID: <%= session.getId() %>
        <br><br>
        
        IsNew: <%= session.isNew() %>
        <br><br>
        
        MaxInactiveInterval: <%= session.getMaxInactiveInterval() %>
        <br><br>
        
        CreateTime: <%= session.getCreationTime() %>
        <br><br>
    
        LastAccessTime: <%= session.getLastAccessedTime() %>
        <br><br>
        
        <% 
            Object username = session.getAttribute("username");
            if(username == null){
                username = "";
            }
        %>
        
        <form action="<%= response.encodeURL("hello.jsp") %>" method="post">
            
            username: <input type="text" name="username" 
                value="<%= username %>"/>
            <input type="submit" value="Submit"/>
        
        </form>
        
    </body>
    </html>

     logout.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>Insert title here</title>
    </head>
    <body>
        
        SessionID: <%= session.getId() %>
        <br><br>
        
        IsNew: <%= session.isNew() %>
        <br><br>
        
        MaxInactiveInterval: <%= session.getMaxInactiveInterval() %>
        <br><br>
        
        CreateTime: <%= session.getCreationTime() %>
        <br><br>
    
        LastAccessTime: <%= session.getLastAccessedTime() %>
        <br><br>
        
        Bye: <%= session.getAttribute("username") %>
        <br><br>
        
        <a href="login.jsp">重新登录</a>    
        
        <% 
            session.invalidate();
        %>
        
    </body>
    </html>
    All that work will definitely pay off
  • 相关阅读:
    Java 重写(Override)与重载(Overload)
    【MyBatis】-----【MyBatis】---表级联系【一对一】--增删改查
    【MyBatis】----【MyBatis】--封装---别名---properties
    【MyBatis】-----初识【MyBatis】
    【Html5】表单全选、全不选
    【SSH】---【Struts2、Hibernate5、Spring4】【SSH框架整合笔记 】
    rabbitmq 消息持久化之receive and send
    git
    Tyrion中文文档(含示例源码)
    计算器源码
  • 原文地址:https://www.cnblogs.com/afangfang/p/12742926.html
Copyright © 2011-2022 走看看