三(9)、JSP九大內置對象——pageContext

pageContext

pageContext對象提供了對jsp頁面內所有的對象及名字空間訪問,比如可以訪問本頁面的session,取本頁面所在的application的某一屬性值,它是頁面所有功能集大成者。pageContext對象可以訪問application對象、exception對象和session對象。還可以直接訪問綁定在application對象、page對象、request對象、session對象上的Java對象。


一、API中

 JspWriter getOut()  返回當前客戶端響應被使用的JspWriter流(out)  
 HttpSession getSession()  返回當前頁中的HttpSession對象(session)  
 Object getPage()  返回當前頁的Object對象(page)  
 ServletRequest getRequest()  返回當前頁的ServletRequest對象(request)  
 ServletResponse getResponse()  返回當前頁的ServletResponse對象(response)  
 Exception getException()  返回當前頁的Exception對象(exception)  
 ServletConfig getServletConfig()  返回當前頁的ServletConfig對象(config)  
 ServletContext getServletContext()  返回當前頁的ServletContext對象(application)  
 void setAttribute(String name,Object attribute)  設置屬性及屬性值 ,在page範圍內有效  
 void setAttribute(String name,Object obj,int scope)  在指定範圍內設置屬性及屬性值 ,int1=page,2=request,3=session,4=application  
 public Object getAttribute(String name)  取屬性的值  
 Object getAttribute(String name,int scope)  在指定範圍內取屬性的值  
 public Object findAttribute(String name)  尋找一屬性,返回起屬性值或NULL  
 void removeAttribute(String name)  刪除某屬性  
 void removeAttribute(String name,int scope)  在指定範圍刪除某屬性  
 int getAttributeScope(String name)  返回某屬性的作用範圍  
 Enumeration getAttributeNamesInScope(int scope)  返回指定範圍內可用的屬性名枚舉  
 void release() 釋放pageContext所佔用的資源  
 void forward(String relativeUrlPath)  使當前頁面重導到另一頁面  
 void include(String relativeUrlPath)  在當前位置包含另一文件  

二、用法

1.獲取已經保存的在session中的username

<%=pageContext.getSession().getAttribute("username")
%>

2.跳轉到login.jsp頁面

<%
pageContext.forward("login.jsp");
%>

3.在當前頁面包含“welcome.jsp頁面的內容”

<% 
pageContext.include("welcome.jsp");
%>

4.查找各個域中的”username”屬性

<% 
pageContext.findAttribute("username");
%>

5.直接解除application對象、session對象、request對象和page對象某個特定的參數或者java對象的綁定關係

<!--在application中解除username值的綁定-->
<%
pageContext.removeAttribute(“username”,pageContext.APPLICATION_SCOPE);
%>
<!--在session中解除username值的綁定-->
<%
pageContext.removeAttribute(“username”,pageContext.SESSION_SCOPE);
%>

6.void setAttribute(String name,Object obj,int scope) 在指定範圍內設置屬性及屬性值 1=page, 2=request, 3=session, 4=application

(1)int scop是由PageContext類提供的靜態變量規定的有以下參數:
1).pageContext.PAGE_SCOPE:頁面範圍page
2).pageContext.REQUEST_SCOPE:請求範圍request
3).pageContext.SESSION_SCOPE:請求範圍session
4).pageContext.APPLICATION_SCOPE:請求範圍application

(2)Object obj是參數,String name。將obj值賦值給name中
(3)例子

  <body>
<%
/* 等同pageContext.setAttribute("name","我是76101",pageContext.PAGE_SCOPE)*/
pageContext.setAttribute("name","我是76101");

/* 等同pageContext.setAttribute("name","我是76102",pageContext.REQUEST_SCOPE)*/
request.setAttribute("name","我是76102");

/*等同pageContext.setAttribute("name","我是76103",pageContext.SESSION_SCOPE)*/
session.setAttribute("name","我是76103");

/*等同pageContext.setAttribute("name","我是76104",pageContext.APPLICATION_SCOPE)*/
application.setAttribute("name","我是76104");
%>
<!-- 取不同的值需要用到相應的方法 -->
page設置的值:<%=pageContext.getAttribute("name") %><br>
request設置的值:<%=pageContext.getRequest().getAttribute("name") %> <br> 
session設置的值:<%=pageContext.getSession().getAttribute("name") %> <br>
application設置的值:<%=pageContext.getServletContext().getAttribute("name") %><br>

<!-- 1=page, 2=request, 3=session, 4=application  -->
範圍1內的值:<%=pageContext.getAttribute("name",1)%><br>
範圍2內的值:<%=pageContext.getAttribute("name",2)%><br>
範圍3內的值:<%=pageContext.getAttribute("name",3)%><br>
範圍4內的值:<%=pageContext.getAttribute("name",4)%><br> 

<!-- 查找的時候是第一個位置 -->
值的查找:<%=pageContext.findAttribute("name") %><br>
屬性name的範圍:<%=pageContext.getAttributesScope("name")%>
</body>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章