1. 程式人生 > >javaweb之jsp的屬性範圍

javaweb之jsp的屬性範圍

.com body 服務 scheme 運行 分享圖片 示例 w3c eat

1.什麽是jsp的屬性範圍?

所謂的屬性範圍就是一個屬性設置之後,可以經過多少個其他頁面後仍然可以保存並繼續使用。jsp提供了四種屬性範圍,如下:

  • 當前頁,對應的jsp對象為pageContext,屬性的作用範圍僅限於當前JSP頁面,跳轉到其他頁面無法取得。
  • 一次服務請求,對應的jsp對象為request,屬性的作用範圍僅限於同一個請求。
  • 一次會話(瀏覽器打開到關閉為一次會話),對應的jsp對象為session,一個用戶設置的內容,只要是與此用戶相關的頁面都可以訪問。
  • 上下文,對應的jsp對象為application,屬性的作用範圍限於當前web應用,是範圍最大的屬性作用範圍,只要在一處設置屬性,在其他各處的JSP或Servlet中都可以獲取到。

2.屬性的操作方法

public void setAttribute(String name,Object value)    設置屬性

public object getAttribute(String name)    取得屬性

public void removeAttribute(String name)    刪除屬性

示例代碼如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP ‘attrTest1.jsp‘ starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
    <%
	pageContext.setAttribute("pageContextAttr", "pageContextValue");
    request.setAttribute("requestAttr", "requestValue");
    session.setAttribute("sessionAttr", "sessionValue");
    application.setAttribute("applicationAttr", "applicationValue");
    %>
    <br><br>
    pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
    <br><br>
    request: <%=request.getAttribute("requestAttr") %>
    <br><br>
    session: <%=session.getAttribute("sessionAttr") %>
    <br><br>
    application: <%=application.getAttribute("applicationAttr") %>
  </body>
</html>

運行後輸出結果如下:

技術分享圖片

3.四種屬性範圍的具體介紹

示例代碼如下:

attrTest1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP ‘attrTest1.jsp‘ starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
    <%
	pageContext.setAttribute("pageContextAttr", "pageContextValue");
    request.setAttribute("requestAttr", "requestValue");
    session.setAttribute("sessionAttr", "sessionValue");
    application.setAttribute("applicationAttr", "applicationValue");
    %>
    <h2>attrTest1 page</h2>
    <br>
    pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
    <br><br>
    request: <%=request.getAttribute("requestAttr") %>
    <br><br>
    session: <%=session.getAttribute("sessionAttr") %>
    <br><br>
    application: <%=application.getAttribute("applicationAttr") %>
    <br><br>
    <a href="jspTest/attrTest2.jsp">To attrTest2 Page</a>
  </body>
</html>

attrTest2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP ‘attrTest2.jsp‘ starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
  	<h2>attrTest2 page</h2>
    <br>
    pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
    <br><br>
    request: <%=request.getAttribute("requestAttr") %>
    <br><br>
    session: <%=session.getAttribute("sessionAttr") %>
    <br><br>
    application: <%=application.getAttribute("applicationAttr") %>
    <br><br>
  </body>
</html>

運行後跳轉頁面顯示結果如下:

技術分享圖片

從上面的運行結果來看,pageContext屬性的作用範圍僅限於當前JSP頁面,從attrTest1頁面跳轉到attrTest2頁面之後,pageContext對象獲得的屬性值為null。

修改上面的代碼:

attrTest1.jsp中,做如下修改:

<h2>attrTest1 page: <%=new Date() %></h2>

attrTest2.jsp中,做如下修改:

<h2>attrTest2 page:<%=new Date() %></h2>

運行attrTest1.jsp後:

技術分享圖片

跳轉到attrTest2頁面:

技術分享圖片

從上面的運行結果可以看出,request的屬性作用範圍僅限於同一個請求,對於不同頁面的不同請求的Date函數的調用,輸出的時間是不同的。

運行attrTest1.jsp:

技術分享圖片

跳轉到attrTest2 頁面時:

技術分享圖片

關閉瀏覽器,直接運行attrTest2.jsp,看到session的屬性值為null,運行結果顯示如下:

技術分享圖片

從上面的運行結果可以看出,在一次會話(session)過程中,在第一個頁面上設置的屬性,跳轉(服務器跳轉/客戶端跳轉)到其他頁面之後,其他的頁面依然可以取得第一個頁面上設置的屬性。會話結束後,屬性值丟失(體現為session值為null)。

javaweb之jsp的屬性範圍