javaWEB总结(13):域对象的属性操作

前言

本文主要讲解javaweb的四个对象以及他们的作用范围,后面会有小demo来具体测试。


四个域对象

(1)pageContext:属性的作用范围仅限于当前JSP页面;

(2)request:属性的作用范围仅限于同一个 请求;

(3)session:属性的作用范围仅限于一次会话,游览器打开直到关闭为一次会话(前提是在此期间会话不会失效)

(4)application:属性的作用范围限于当前WEB应用。


域对象共有的方法:

(1)Object getAttribute(String name):获取指定属性;

(2)Enumeration getAttributeNames():获取所有属性的名字组成的Enumeration对象;

(3)removeAttribute(String name):移除指定属性;

(4)void setAttribute(String name,Object o):设置属性。



项目结构



web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWeb_13</display-name>
  <welcome-file-list>
    <welcome-file>first.jsp</welcome-file>
  </welcome-file-list>
</web-app>

first.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>当前页获取域对象里面的属性值</title>
</head>
<body>

	<h2>First Page</h2>

	<% 
	pageContext.setAttribute("pageContextAttr", "pageContextValue"); 
	
	request.setAttribute("requestAttr", "requestValue");
	
	session.setAttribute("sessionAttr", "sessionValue");
	
	application.setAttribute("applicationAttr", "applicationValue");
	%>
	
	01-pageContext:<%=pageContext.getAttribute("pageContextAttr") %>
	<br><br>
	02-request:<%=request.getAttribute("requestAttr") %>
	<br><br>
	03-session:<%=session.getAttribute("sessionAttr") %>
	<br><br>
	04-application:<%=application.getAttribute("applicationAttr") %>
	<br><br>
	
	<a href="second.jsp">To Second Page</a>
	
</body>
</html>

second.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>跨页面获取域对象里面的属性值</title>
</head>
<body>

	<h2>Second Page</h2>

	01-pageContext:<%=pageContext.getAttribute("pageContextAttr") %>
	<br><br>
	02-request:<%=request.getAttribute("requestAttr") %>
	<br><br>
	03-session:<%=session.getAttribute("sessionAttr") %>
	<br><br>
	04-application:<%=application.getAttribute("applicationAttr") %>
	<br><br>
	
	<a href="first.jsp">To First Page</a>
	
</body>
</html>


运行项目后,在当前页面里面可以取到所有域对象的值


跨页面时,由于是不同的页面,不同的请求,所以pageContext和request为空



关闭游览器,再次重新打开,直接输入第二个页面的地址,session也为空,说明再次打开游览器后不是同一次会话




参考视频:点击打开链接

转载于:https://www.cnblogs.com/tongrui/p/6275379.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值