验证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