【web开发】四个域对象

本文详细介绍了Web开发中的四个域对象:PageContext、HttpServletRequest、HttpServletSession和ServletContext,包括它们的范围、获取方法以及setAttribute和getAttribute操作。ServletContext的测试代码展示了设置和获取属性的过程。Session的生命周期和作用被讨论,指出它在会话期间存储数据。PageContext作为最小的域对象,主要负责获取其他隐式对象和跨域数据。Request域在请求链中起到数据共享的作用。
摘要由CSDN通过智能技术生成

一、域对象

范围由小到大:page   request   session   application

servlet的那4个域分别对应的是4个Class(详见api,javax.servlet.jsp.PageContext)
page--PageContext
request--HttpServletRequest
session--HttpServletSession
application--ServletContext

request,session,application都是基于page扩展的对象
所以可以根据page(pageContext)来获取任一一个域的对象
getPage()
getRequest()
getSession()
getServletContext()

每个域对象都提供了setAttribute getAtribute的方法 ,用于保存各自存储属性的范围
getPage().getAttribute()/setAttribute()
getRequest().getAttribute()/setAttribute()
getSession().getAttribute()/setAttribute()
getServletContext().getAttribute()/setAttribute()

 

二、实例

ServletContext的测试代码及结果:

设置属性的代码如下:

获取属性的代码如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值