1. 程式人生 > >Jsp中的PageContext隱式物件詳解

Jsp中的PageContext隱式物件詳解

問題?關於jsp的九大隱式物件中的PageContext詳解

pageContextjavax.servlet.jsp.PageContext非常重要

一、pageContext有三大作用:


1、本身是一個域物件。同時還能操作其他三個域物件(PageContext ServletRequest HttpSession           ServletContext)本身表示的域範圍是本頁面。


void setAttribute(String name,Object value)
void removeAttribute(String name)
Object getAttribute(String name)


操作其他的三個域物件
void setAttribute(String name,Object value,int scope)//scope操作於其他三大域物件
void removeAttribute(String name,int scope)
Object getAttribute(String name,int scope)

2.引數int scope是由PageContext類提供的靜態變數規定的有以下引數:
PageContext.PAGE_SCOPE:頁面範圍(是PageContext本身中的那個Map,代號page)


PageContext.REQUEST_SCOPE:請求範圍(是ServletRequest中的那        個Map,代號request)


PageContext.SESSION_SCOPE:請求範圍(是HttpSession中的那個                Map,代號session)


PageContext.APPLICATION_SCOPE:請求範圍(是ServletContext中        的那個Map,代號application)

舉個例子(jsp頁面):

PageContext1.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 'PageContext1.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只在本頁面有效,在其他頁面使用pageContext是不能取不到值的
    //pageContext.setAttribute("p1", "p的第一個值");
    pageContext.setAttribute("p1", "p的第二個值",PageContext.PAGE_SCOPE);
    //可是如果用HttpRequest呢?
     //request.setAttribute("p2", "p2");
    pageContext.setAttribute("p2", "p22",PageContext.REQUEST_SCOPE);
    
    // session.setAttribute("p3", "p3");
    pageContext.setAttribute("ps","p33",PageContext.SESSION_SCOPE);
    
    //application.setAttribute("p4", "p4");
    pageContext.setAttribute("p4", "p44",PageContext.APPLICATION_SCOPE);
    
    
    pageContext.forward("/PageContext2.jsp");
    //response.sendRedirect("/day09/PageContext2.jsp");
    %>
  
  </body>
</html>
PageContext2.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 'PageContext2.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.getAttribute("p")//得到PageContext1.jsp頁面p的值?試問可以得到嗎?大家想想
    %>
    <%=application.getAttribute("p") %><!-- 那大家想想這裡取得到上一頁面的值嗎? -->
    <%=pageContext.findAttribute("p") %>
  </body>
</html>

執行看看結果如何?

注意: 轉發   請求重定向   關瀏覽器  關伺服器   結果都會變(這個很重要)
(非常有用)Object findAttribute(String name):依次按照page    request   session     application範圍搜尋指定名稱的物件,找到為止。

2、獲取其他8個隱式物件

舉個例子(普通類中):

package itcat;

import javax.servlet.ServletContext;
import javax.servlet.jsp.PageContext;

public class pagecontext {
	public void m(PageContext pag){
		//如果你要傳參9大隱式物件,不可能寫9個參量吧!
		//這個時候我們的Context的作用就可以了,它可以呼叫其他8個隱式物件,直接可定義這一個
		//這樣就方便很多了。
		ServletContext ser=pag.getServletContext();
		pag.getServletConfig();
		pag.getOut();
		pag.getPage();
		pag.getRequest();
		pag.getResponse();
		pag.getSession();
		pag.getException();
		//這就是其他8個隱式物件了,都可以通過PageContext得到,非常的方便
	}
}

3、提供了轉發和包含的方便方法
RequestDispatcher rd = request.getRequestDispatcher("/url");//“/”絕對路徑
rd.forward(request,response);

pageContext.forward("url");//重定向
pageContext.include("url");//包含