JAVAEE之內置對象和屬性範圍
內置對象和屬性範圍
- 四種屬性範圍
- 九個內置對象
1.內置對象
如果說想要使用一個對象,必須new 出來,但是在我們的jsp操作中,發現我們使用過的out,request對象沒有進行實例化,類似於這樣的對象,我們叫做jsp的內置對象,也就是收不需要實例化的對象,總共有9個。
序號 |
對象 |
類型 |
1 |
pageContext |
javax.servlet.jsp.PageContext |
2 |
request |
javax.servlet.http.HttpServletRequest |
3 |
response |
javax.servlet.http.HttpServletResponse |
4 |
session |
javax.servlet.http.HttpSession |
5 |
application |
javax.servlet.ServletContext |
6 |
config |
javax.servlet.ServletConfig |
7 |
out |
javax.servlet.jsp.jspWriter |
8 |
page |
java.lang.Object |
9 |
exception |
java.lang.Throwable |
2.屬性範圍
在jsp中,有四種屬性範圍,所謂屬性範圍,就是指在一個jsp中設置了屬性,是否可以在其他頁面能夠訪問。四種屬性範圍分別指的是以下的四種:
- ·屬性範圍在當前頁面有效:設置了一個屬性,在設置屬性的頁面可以取得屬性,跳轉到其他頁面則無法取得設置的屬性
- ·屬性範圍在服務端跳轉範圍內有效:一個頁面設置了屬性,然後經過服務端跳轉(n次),依然可以取得設置的屬性
- ·屬性範圍在一次會話中有效:一個頁面設置屬性,無論是經過服務端還是客戶端跳轉,只要瀏覽器不重啟,設置的屬性都可以取得。
- ·屬性範圍在服務器上下文中有效:頁面設置的屬性,無論是經過客戶端還是服務端跳轉,無論是哪一個會話,只要服務器不重啟,設置的屬性都可以訪問。
NO |
方法 |
描述 |
1 |
public void setAttribute(String name,Object val) |
設置屬性 |
2 |
pulblic Object getAttribute(String name) |
取得屬性 |
3 |
public void removeAttribute(String name) |
刪除屬性 |
2.1page屬性範圍
page屬性範圍也就是我們上面所提到的第一種屬性範圍,使用的是pageContext這個內置對象設置和取得的,指的是在一個頁面使用pageContext設置了屬性之後,本頁面可以通過pageContext取得設置的屬性,跳轉到其他頁面之後無法取得設置的屬性。要註意的一點,所謂的page屬性範圍使用的是pageContext這個內置對象設置和取得的。
<[email protected] contentType="text/html; charset=utf-8"%> <% //設置屬性 pageContext.setAttribute("company","廈門"); pageContext.setAttribute("address","廈門市"); pageContext.setAttribute("tel","0592-2565166"); %> <!-- 取得設置的屬性 --> <h1><%=pageContext.getAttribute("company") %></h1> <h1><%=pageContext.getAttribute("address") %></h1> <h1><%=pageContext.getAttribute("tel") %></h1>
如果現在我們通過服務端跳轉,看能不能取得設置的屬性
<[email protected] contentType="text/html; charset=utf-8"%> <!-- 取得設置的屬性 --> <h1><%=pageContext.getAttribute("company") %></h1> <h1><%=pageContext.getAttribute("address") %></h1> <h1><%=pageContext.getAttribute("tel") %></h1> <h1>這是pageAttributeDemo02.jsp</h1>
發現我們通過服務端跳轉後取不到之前設置的page範圍的屬性了,如果說想要通過服務端跳轉後也能取得設置的屬性,我們必須使用第二種屬性範圍。
2.2request屬性範圍
request屬性範圍,是第二種屬性範圍,也就是說設置的屬性在當前頁面可以取得,通過服務端跳轉之後也可以取得設置的屬性。
<[email protected] contentType="text/html; charset=utf-8"%> <% //設置屬性 request.setAttribute("company","廈門"); request.setAttribute("address","廈門市"); request.setAttribute("tel","0592-2565166"); %> <!-- 取得設置的屬性 --> <h1><%=request.getAttribute("company") %></h1> <h1><%=request.getAttribute("address") %></h1> <h1><%=request.getAttribute("tel") %></h1> <jsp:forward page="pageAttributeDemo02.jsp"></jsp:forward>
<[email protected] contentType="text/html; charset=utf-8"%> <!-- 取得設置的屬性 --> <h1><%=request.getAttribute("company") %></h1> <h1><%=request.getAttribute("address") %></h1> <h1><%=request.getAttribute("tel") %></h1> <h1>這是pageAttributeDemo02.jsp</h1>
通過
<[email protected] contentType="text/html; charset=utf-8"%> <% //設置屬性 request.setAttribute("company","廈門"); request.setAttribute("address","廈門"); request.setAttribute("tel","0592-2565166"); %> <!-- 取得設置的屬性 --> <h1><%=request.getAttribute("company") %></h1> <h1><%=request.getAttribute("address") %></h1> <h1><%=request.getAttribute("tel") %></h1> <jsp:forward page="requestAttributeDemo02.jsp"></jsp:forward>
現在,我們發現,request範圍屬性經過多次跳轉之後,依然可以取得設置的屬性,那麽現在如果經過客戶端跳轉呢?
<[email protected] contentType="text/html; charset=utf-8"%> <% //設置屬性 request.setAttribute("company","廈門"); request.setAttribute("address","廈門"); request.setAttribute("tel","0592-2565166"); %> <!-- 取得設置的屬性 --> <h1><%=request.getAttribute("company") %></h1> <h1><%=request.getAttribute("address") %></h1> <h1><%=request.getAttribute("tel") %></h1> <a href="requestAttributeDemo03.jsp">客戶端跳轉</a>
<[email protected] contentType="text/html; charset=utf-8"%> <!-- 取得設置的屬性 --> <h1><%=request.getAttribute("company") %></h1> <h1><%=request.getAttribute("address") %></h1> <h1><%=request.getAttribute("tel") %></h1> <h1>這是requestAttributeDemo03.jsp</h1>
通過程序運行結果發現,通過客戶端跳轉不能夠取得設置的request範圍的屬性,那麽如果現在我就想要通過客戶端取得設置的屬性呢?那麽我們可以使用第三種範圍屬性。
3.session
session屬性範圍就是我們第三種屬性範圍,也就是之前提到過的,屬性範圍在一次會話中有效。是無論經過客戶端還是服務端跳轉,都可以取得設置的屬性,當然前提是瀏覽器不能重啟。
<[email protected] contentType="text/html; charset=utf-8"%> <% //設置屬性 session.setAttribute("company","廈門"); session.setAttribute("address","廈門"); session.setAttribute("tel","0592-2565166"); %> <!-- 取得設置的屬性 --> <h1><%=session.getAttribute("company") %></h1> <h1><%=session.getAttribute("address") %></h1> <h1><%=session.getAttribute("tel") %></h1> <a href="sessionAttributeDemo03.jsp">客戶端跳轉</a>
<[email protected] contentType="text/html; charset=utf-8"%> <!-- 取得設置的屬性 --> <h1><%=session.getAttribute("company") %></h1> <h1><%=session.getAttribute("address") %></h1> <h1><%=session.getAttribute("tel") %></h1> <h1>02</h1>
<[email protected] contentType="text/html; charset=utf-8"%> <!-- 取得設置的屬性 --> <h1><%=session.getAttribute("company") %></h1> <h1><%=session.getAttribute("address") %></h1> <h1><%=session.getAttribute("tel") %></h1> <h1>03</h1>
以上的程序說明我們session範圍的屬性,在經過客戶端或者服務端跳轉之後,設置的屬性依然可以取得。現在我們來重啟一下瀏覽器:通過操作發現,重啟瀏覽器之後,取不到設置的session範圍屬性了,那麽如果現在想要在瀏覽器重啟後取得設置的屬性,我們必須使用第四種屬性範圍。
4.application屬性範圍
application範圍屬性就是第四種範圍屬性,屬性是設置在服務器的上下文中,只要服務器不重啟,那麽設置的屬性統統都可以取得。無論是經過客戶端你還是服務端還是瀏覽器重啟,都可取得設置的屬性。
<[email protected] contentType="text/html; charset=utf-8"%> <% //設置屬性 application.setAttribute("company","廈門"); application.setAttribute("address","廈門"); application.setAttribute("tel","0592-2565166"); %> <!-- 取得設置的屬性 --> <h1><%=application.getAttribute("company") %></h1> <h1><%=application.getAttribute("address") %></h1> <h1><%=application.getAttribute("tel") %></h1> <a href="applicationAttributeDemo03.jsp">客戶端跳轉</a>
<[email protected] contentType="text/html; charset=utf-8"%> <!-- 取得設置的屬性 --> <h1><%=application.getAttribute("company") %></h1> <h1><%=application.getAttribute("address") %></h1> <h1><%=application.getAttribute("tel") %></h1> <h1>03</h1>
設置的application範圍的屬性,無論是通過客戶端還是服務端或者瀏覽器重啟,都可以取得設置的屬性。那麽現在如果服務器重啟呢?通過操作發現,重啟服務器後,之前設置的屬性取不到了,那麽如果想要在重啟服務器後取得設置的屬性呢?那是沒有辦法的。
3.屬性範圍使用說明
在開發中,我們常用的屬性範圍就是request和session範圍,那麽在開發中,怎麽區分使用二者呢?一般來說,只對於同一個功能的屬性的傳遞,使用request範圍屬性。那麽對於session範圍來說,主要還是用於登陸上面。
4.關於pageContext屬性的進一步說明
上面我們講解的是四種屬性範圍,實際上四種屬性範圍都是通過pageContext設置的,在pageContext中,setAttribute()方式實際上被重載過:
·設置屬性:
·public abstract void setAttribute(java.lang.String name, java.lang.Object value)
·public abstract void setAttribute(java.lang.String name,
java.lang.Object value,
int scope)
對於這兩個方法,第一個方法是我們之前一直在使用的,對於第二個方法,相比第一個方法多了一個參數(int scope),實際上這個參數就是指定屬性範圍的參數:
·APPLICATION_SCOPE
·PAGE_SCOPE
·SESSION_SCOPE
·REQUEST_SCOPE
也就是說,通過這個方法,我們可以設置四種屬性範圍。
<[email protected] contentType="text/html; charset=utf-8"%> <% pageContext.setAttribute("j1m","111",PageContext.SESSION_SCOPE); %> <a href="pageContextDemo02.jsp">跳轉</a>
<[email protected] contentType="text/html; charset=utf-8"%> <h1>02</h1> <h1><%=session.getAttribute("jjm") %></h1>
總結,知道九個內置對象(pageContext,session,request,response,applicatino,config,page,exception),充分掌握四種屬性範圍(page僅對於自己的頁面傳值;request應用於服務端傳值,但是客戶端跳轉不能傳值;session一般應該於用戶的登錄註冊保存在一次會話中,會話結束,屬性值消失;application屬性範圍應用於整個服務器中,服務器關閉屬性值消失。
JAVAEE之內置對象和屬性範圍