01-Cookie&Session3

简介: 01-Cookie&Session3

配置

如果说。你希望你的web工程,默认的session的超时时长为其他时长。你可以在你自己的web.xml配置文件中做 以上相同的配置。就可以修改你的web工程所有Seession的默认超时时长。

<!--表示当前web工程。创建出来的所有Session默认是20分钟超时时长-->
<session-config>
  <session-timeout>20</session-timeout> 
</session-config>

结果

session3秒超时销毁

如果你想只修改个别session的超时时长。就可以使用上面的API setMaxInactiveInterval来进行单独的设置。

session.setMaxlnactivelnterval(int interval)单独设置超时时长。

超时的说明

修改 session.html

<!DocTYPE 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>Session</title>
    <base href="http://localhost:8080/13_cookie_session/">
    <style type="text/css">
        ul li{
            list-style:none;
        }
    </style>
</head>
<body>
    <iframe name="target" width="500" height="500" style="..."></iframe>
    <div style="...">
        <ul>
            <li><a href="sessionServlet?action=createOrGetSession" 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="sessionServlet?action=defaultLife" target="target">Session的默认超时及配置</a></li>
                    <li><a href="sessionServlet?action=life3" 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>

修改 SessionServlet

package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionServlet extends BaseServlet{
    protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建和获取Session会话对象
        HttpSession session = req.getSession();
        //判断 当前session回滚,是否是新创建出来的
        boolean isNew = session.isNew();
        //获取session会话的唯一标识 id
        String id = session.getId();
        resp.getWriter().write("得到的Session,它的id是:"+id+"<br/>");
        resp.getWriter().write("这个Session是否是新创建的:"+isNew+"<br/>");
    }
    /**
     * 在Session中保存数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.getSession().setAttribute("key1","value1");
       resp.getWriter().write("已经往Session中保存了数据");
    }
    /**
     * 获取Session域中的数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object attribute = req.getSession().getAttribute("key1");
        resp.getWriter().write("从Session中获取key1的数据是"+attribute);
    }
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取了Session的默认超时时长
        int maxInactiveInterval = req.getSession().getMaxInactiveInterval();
        resp.getWriter().write("Session的默认超时时长是:"+maxInactiveInterval+"秒");
    }
    protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取Session对象
        HttpSession session = req.getSession();
        //设置当前Session3秒后超时
        session.setMaxInactiveInterval(3);
        resp.getWriter().write("当前Session已经设置为3秒后超时");
    }
}

结果

创建新的true->获取是否为新创建false–>3秒销毁—>获取是否为新创建true



Session马上销毁

修改 SessionServlet

package com.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionServlet extends BaseServlet{
    protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建和获取Session会话对象
        HttpSession session = req.getSession();
        //判断 当前session回滚,是否是新创建出来的
        boolean isNew = session.isNew();
        //获取session会话的唯一标识 id
        String id = session.getId();
        resp.getWriter().write("得到的Session,它的id是:"+id+"<br/>");
        resp.getWriter().write("这个Session是否是新创建的:"+isNew+"<br/>");
    }
    /**
     * 在Session中保存数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void setAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.getSession().setAttribute("key1","value1");
       resp.getWriter().write("已经往Session中保存了数据");
    }
    /**
     * 获取Session域中的数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getAttribute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object attribute = req.getSession().getAttribute("key1");
        resp.getWriter().write("从Session中获取key1的数据是"+attribute);
    }
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取了Session的默认超时时长
        int maxInactiveInterval = req.getSession().getMaxInactiveInterval();
        resp.getWriter().write("Session的默认超时时长是:"+maxInactiveInterval+"秒");
    }
    protected void life3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取Session对象
        HttpSession session = req.getSession();
        //设置当前Session3秒后超时
        session.setMaxInactiveInterval(3);
        resp.getWriter().write("当前Session已经设置为3秒后超时");
    }
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先获取Session对象
        HttpSession session = req.getSession();
        //让Session对象马上超时
        session.invalidate();
        resp.getWriter().write("Session已经设置为超时(无效)");
    }
}

修改 session.html

<!DocTYPE 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>Session</title>
    <base href="http://localhost:8080/13_cookie_session/">
    <style type="text/css">
        ul li{
            list-style:none;
        }
    </style>
</head>
<body>
    <iframe name="target" width="500" height="500" style="..."></iframe>
    <div style="...">
        <ul>
            <li><a href="sessionServlet?action=createOrGetSession" 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="sessionServlet?action=defaultLife" target="target">Session的默认超时及配置</a></li>
                    <li><a href="sessionServlet?action=life3" target="target">session3秒超时销毁</a></li>
                    <li><a href="sessionServlet?action=deleteNow" target="target">Session马上销毁</a></li>
                </ul>
            </li>
            <li><a href="" target="target">浏览器和Session绑定的原理</a></li>
        </ul>
    </div>
</body>
</html>

结果

创建新的true->获取是否为新创建false–>马上销毁—>获取是否为新创建true





m)浏览器和Session之间关联的技术内幕

视频

session技术,底层其实是基于Cookie技术来实现的。

相关文章
|
算法 5G 调度
5G 多址接入|带你读《5G空口特性与关键技术》之五
采用正交多址方式,用户间相互不存在干扰。采用非正交多址方式,每个用户的信号有可能与其他用户的信号相互叠加干扰,但是这种干扰通常在接收时可以采用信号处理的方式去除,以还原某个特定用户的信号。
16890 3
5G 多址接入|带你读《5G空口特性与关键技术》之五
|
Go 开发工具 Python
【开发工具】Goland 2022.4 破解(by ja-netfilter)
【开发工具】Goland 2022.4 破解(by ja-netfilter)
1122 1
【开发工具】Goland 2022.4 破解(by ja-netfilter)
|
人工智能 数据可视化 网络安全
Dify与DeepSeek的深度融合——构建您的专属AI助手
在当今数据驱动、AI为王的时代,Dify与DeepSeek作为领先的AI开发工具和大模型引擎,为企业和个人提供高效智能的解决方案。Dify是面向AI应用开发的低代码平台,集成预训练模型、可视化界面和无缝部署功能;DeepSeek则是高性能、低成本的开源大语言模型,具备多轮推理能力。两者结合并通过私有化部署,确保数据安全与合规,极大提升开发效率和业务生产力。阿里云计算巢提供了两者的私有化部署方案,帮助用户快速搭建专属AI应用。
1448 1
|
人工智能 运维 自然语言处理
AI Powered SLS 智能分析能力创新
随着云计算技术不断升级,承载业务的 IT 基础设施规模扩大,各个应用之间的链路关系变得越来越复杂,每时每刻都在产生海量级的日志。
99083 118
|
边缘计算 自动驾驶 物联网
5G 如何实现大规模天线阵列:赋予无线通信全新力量
5G 如何实现大规模天线阵列:赋予无线通信全新力量
1253 0
|
人工智能 运维 监控
|
小程序 项目管理 数据安全/隐私保护
基于微信小程序的高校宿舍信息管理系统设计与实现(源码+lw+部署文档+讲解等)
基于微信小程序的高校宿舍信息管理系统设计与实现(源码+lw+部署文档+讲解等)
471 0
基于微信小程序的高校宿舍信息管理系统设计与实现(源码+lw+部署文档+讲解等)
|
存储 缓存 算法
[译] OpenSSL 3.0.0 设计
本文翻译 OpenSSL 官网文档:https://www.openssl.org/docs/OpenSSL300Design.htmlTongsuo-8.4.0 是基于 OpenSSL-3.0.3 开发,所以本文对 Tongsuo 开发者同样适用,内容丰富,值得一读!介绍本文概述了 OpenSSL 3.0 的设计,这是在 1.1.1 版本之后的 OpenSSL 的下一个版本。假设读者熟悉名为 &
392 0
[译] OpenSSL 3.0.0 设计
|
小程序 数据库
|
机器学习/深度学习 存储 人工智能
英特尔推出两款 7nm AI 芯片,内存高达 96GB HBM2e,训练成本更低
当地时间 5 月 10 日,英特尔举办了 2022 英特尔 On 产业创新峰会。在此次峰会上,英特尔公布了其在芯片、软件和服务方面取得的多项进展,并宣布了包括第 12 代英特尔酷睿 HX 处理器家族、阿波罗计划、Greco AI 加速芯片在内的一系列重大发布。
736 0
英特尔推出两款 7nm AI 芯片,内存高达 96GB HBM2e,训练成本更低