JSP内置对象--4种属性范围 (pageContext,request,session,application)

pageContext: javax.servlet.jsp.JspContext抽象类的父类JspContext 中有以下方法。

request:javax.servlet.http.的接口HttpServletRequest

session:javax.servlet.http的接口HttpSession

application:javax.servlet 的接口:ServletContext 

以上4个对象都存在以下3个方法:

设置属性名称是string,内容是object。

  • page范围:

例子: page属性只能当前页面取得:

page_scope_01.jsp:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    pageContext.setAttribute("name","李兴华") ;
    pageContext.setAttribute("birthday",new Date()) ;
%>
<%
    String username = (String) pageContext.getAttribute("name") ;
    Date userbirthday = (Date)pageContext.getAttribute("birthday") ;
%>
<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>
</body>
</html>

服务器跳转后属性内容无法取得,测试:

page_scope_02.jsp:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    pageContext.setAttribute("name","李兴华") ;
    pageContext.setAttribute("birthday",new Date()) ;
%>
<jsp:forward page="page_scope_03.jsp"/>
</body>
</html>

服务器跳转到page_scope_03.jsp:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
    String username = (String) pageContext.getAttribute("name") ;
    Date userbirthday = (Date)pageContext.getAttribute("birthday") ;
%>
<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>
</body>
</html>

结果:

如果想服务器端跳转后属性可以保存下来的话,那就用request属性:

request_scope_01.jsp设置request属性:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    request.setAttribute("name","李兴华") ;
    request.setAttribute("birthday",new Date()) ;
%>
<jsp:forward page="request_scope_02.jsp"/>
</body>
</html>

获取request属性:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
    String username = (String) request.getAttribute("name") ;
    Date userbirthday = (Date)request.getAttribute("birthday") ;
%>
<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>
</body>
</html>

如果一直有服务器跳转,则一直可以传递过去.

如果现在换一种形式,使用超链接进行页面的跳转的话;

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    request.setAttribute("name","李兴华") ;
    request.setAttribute("birthday",new Date()) ;
%>
<a href="request_scope_02.jsp">通过链接取得属性</a>
</body>
</html>

属性值将不被保存,无法传递request属性,因为地址改变了,不属于服务器跳转,而是客户端跳转。

所以,客户端跳转,相当于发生了两次请求,那么第一次请求不复存在。

如果现在希望不管是服务器端还是客户端跳转,都可以保存的话,可以扩大范围到session属性。

session设置属性:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    session.setAttribute("name","李兴华") ;
    session.setAttribute("birthday",new Date()) ;
%>
<a href="session_scope_02.jsp">通过链接取得属性</a>
</body>
</html>

session读取属性:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
    String username = (String) session.getAttribute("name") ;
    Date userbirthday = (Date)session.getAttribute("birthday") ;
%>
<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>
</body>
</html>

可以成功读取,但是如果打开新的浏览器的话,无法取得属性,所以session值保存在当前的浏览器中。

对于服务器而言,每一个连接到它的客户端都是一个session。

如果想让属性设置一次后,不管是否是新的浏览器打开都可以访问的话,就可以扩大到application属性“:

所有application属性都保存在服务器上,所有的用户(每一个session)都可以访问:

application设置属性:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    application.setAttribute("name","李兴华") ;
    application.setAttribute("birthday",new Date()) ;
%>
<a href="application_scope_02.jsp">通过链接取得属性</a>
</body>
</html>

application获取属性:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%
    String username = (String) application.getAttribute("name") ;
    Date userbirthday = (Date)application.getAttribute("birthday") ;
%>
<h2>姓名:<%=username%></h2>
<h2>生日:<%=userbirthday%></h2>
</body>
</html>

只要是application设置的属性,所有的用户都可以访问,表示公共的内容。

但是如果服务器启动了,则无法取得。因为关闭后属性消失,要想再次访问,重新设置。

既然application属性范围都保存在服务器上,如果同时设置很多个application的话,服务器的性能会降低,而4种属性范围都是保存在服务器上的,这时给出一个车原则,能使用page的,不使用request,能适用request的,不使用session,能使用session的,不用application。因为保存范围越窄,对服务器的压力越低。

深入研究page范围:

page范围通过pageContext对象设置。但是在pageContext里有如下设置属性的方法:

public void setAttribute(String name, Object value, int scope)

scope有4个表示属性范围的常量可以设置:

1. PAGE_SCOPE

2. REQUEST_SCOPE

3. SESSION_SCOPE

4. APPLICATION_SCOPE

测试例子:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<body>
<%    // 设置属性
    pageContext.setAttribute("name","李兴华",PageContext.REQUEST_SCOPE) ;
    pageContext.setAttribute("birthday",new Date(),PageContext.REQUEST_SCOPE) ;
%>
<jsp:forward page="request_scope_02.jsp"/>
</body>
</html>

以上通过pageContext将一个属性设置成request范围,所以服务器跳转时属性可以保存。

所以注意:::pageContext可以操作4种范围,这一点直接决定以后的开发问题。

以上4种范围是整个web开发的核心。

原文地址:https://www.cnblogs.com/wujixing/p/4950145.html