spring boot 整合thyemleaf基本使用 域对象操作(六)

本文探讨了在Web应用程序中使用request、session和application作用域来管理数据的方法。详细讲解了request对象用于临时存储请求数据,session对象保持用户会话状态直至浏览器关闭,而application对象在整个Web应用中共享数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
      Request:<span th:text="${#httpServletRequest.getAttribute('red')}"></span><br/>
      Session:<span th:text="${session.sess}"></span><br/>
      Application:<span th:text="${application.app}"></span><br/>
</body>
</html>

 

controller 文件:

@RequestMapping("show5")
public String showInfo5(HttpServletRequest request,Model model){
   request.setAttribute("req","HttpServletRequest");
   request.getSession().setAttribute("sess","HttpSession");
   request.getSession().getServletContext().setAttribute("app","Application");

    return "index5";
}

预备知识点:1、request.getSession()可以帮你得到HttpSession类型的对象,通常称之为session对象session对象的作用域为一次会话,通常浏览器不关闭,保存的值就不会消失,当然也会出现session超时。服务器里面可以设置session的超时时间,web.xml中有一个session time out的地方,tomcat默认为30分钟。

2、(1)servletContext就是代表web应用的一个servlet类。也就是说一个web应用只有一个servletContext对象。
(2)servletContext是用来管理web应用下的资源的。
(3)servletContext是可以被所有访问其代表web应用的用户共享的。
(4)servletContext是在服务器端被创建和销毁的。
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值