验证pageContext、request、session、application的作用域

验证pageContext、request、session、application的作用域

首先我们先创建一个javaWeb项目,做好相关资源的配置。
创建一个主页面index.jsp

	<%
		//此页面用来查看  作用域   
		//1.request       请求,通常用于form表单或地址栏中的数据请求
		//2.session       一次会话
		//3.pageContext   当前页对象
		//4.application	  全局对象(上下文对象)
		//验证方式:分别给不同的对象设置不同的参数,跳转观察不同。
		request.setAttribute("request", "这是request");
		session.setAttribute("session", "这是session");
		application.setAttribute("application", "这是application");
		pageContext.setAttribute("pageContext", "这是pageContext");

	%>
	//此处是个简单的跳转按钮,跳转页面的代码在下面
	<a href="other.jsp">跳转</a><hr>
	//获取上方设置的值并做好前面的标注
	pageContext:<%=pageContext.getAttribute("pageContext") %><br>
	request:<%=request.getAttribute("request") %><br>
	session:<%=session.getAttribute("session") %><br>
	application:<%=application.getAttribute("application") %>

创建other.jsp

<body>
	<%
		//由于request是请求,只有请求的时候才会携带数据,既方法
		//request.getRequestDispatcher("").forward(request, response);
		//所以跳转后为null
		//pageContext指的是当前页面,所以跳转后为null;
		//request.getRequestDispatcher("").forward(request, response);
	%>
	
	pageContext:<%=pageContext.getAttribute("pageContext") %><br>
	request:<%=request.getAttribute("request") %><br>
	session:<%=session.getAttribute("session") %><br>
	application:<%=application.getAttribute("application") %>
</body>

运行在服务器上,直接打开index.jsp文件
在这里插入图片描述
点击跳转后,会出现
在这里插入图片描述
如果是直接打开other.jsp
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值