使用ExternalContext類提供的方法可以獲取ServletContext
可使用下列方法獲取servlet信息
● getContext 此方法可獲取Web應用中與當前請求相關聯的ServletContext對象
public abstract Object getContext()
● getRequest 此方法可獲取代表當前正在處理的請求的ServletRequest對象
public abstract Object getRequest()
● getResponse 此方法可獲取代表當前正在呈現的響應的ServletResponse對象
public abstract Object getResponse()
這些方法都是返回一個java
getApplicationMap方法返回一個包含ServletContext對象裡全部特性名/值對的Map對象
public abstract java
作為一個例子
Object contextAttribute = null;
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map contextMap = externalContext
if (contextMap!=null)
contextAttribute = contextMap
通過ExternalContext對象可訪問與當前請求相關聯的Session對象
public abstract Object getSession(boolean create)
此方法其實是javax
getSessionMap方法返回一個包含與當前請求相關聯的Session對象裡所有特性名/值對的Map對象
public abstract java
要獲取Session對象裡的特性
Object sessionAttribute = null;
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map sessionMap = externalContext
if (sessionMap!=null)
sessionAttribute = sessionMap
最後一行的key是一個包含特性名的字符串
getInitParameter方法是ServletContext對象的getInitParameter方法的包裝器
public abstract String getInitParameter(String parameterName)
舉例來說
<context
<param
<param
</context
下面代碼中的字符串變量initParam的值會是Scott Jobim
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
String initParam = externalContext
getInitParameterMap方法返回一個包含ServletContext對象中全部初始參數的Map對象
public abstract java
為了獲取一個初始參數的值
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map paramMap = externalContext
if (paramMap!=null) {
System
}
getRequestMap方法返回一個包含當前Request對象中全部特性名/值對的Map對象
public abstract java
作為一個例子
Object requestAttribute = null;
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map requestMap = externalContext
if (requestMap!=null)
requestAttribute = requestMap
最後一行裡的Key是一個包含要提取的屬性名的字符串
getRequestParameterMap
getRequestParameterMap返回一個包含Request對象裡全部參數名/值對的Map對象
public abstract java
作為一個例子
String id = null;
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map requestParameterMap = externalContext
if (requestParameterMap!=null)
id = (String) requestParameterMap
getRequestParameterNames方法返回一個包含全部請求參數名的Iterator
public abstract java
作為一個例子
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map requestParameterMap = externalContext
Iterator parameterNames = externalContext
while (parameterNames
String parameterName = (String) parameterNames
String parameterValue =
(String) requestParameterMap
System
}
getRequestParameterValuesMap方法返回一個包含Request對象裡全部參數名/值對的Map對象
public abstract java
下面例子中的代碼把請求參數id的全部值輸出到控制台
String[] id = null;
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map requestParameterValuesMap = externalContext
if (requestParameterValuesMap!=null) {
id = (String[]) requestParameterValuesMap
// print all values of id
for (int i=
System
}
}
getRequestHeaderMap方法返回一個包含當前請求中全部頭名/值對的Map對象
public abstract java
舉個例子來說
String host = null;
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map headerMap = externalContext
if (headerMap!=null) {
host = (String) headerMap
System
}
注意
報頭的名稱是不區分大小寫的
getRequestHeaderValuesMap方法與getRequestHeaderMap方法相似
ValuesMap方法返回的Map對象上調用get方法會得到一個字符串的數組
public abstract java
在getRequestHeaderValuesMap方法返回的Map對象上調用get方法會返回一個java
下面的代碼使用getRequestHeaderValuesMap方法來獲取一個包含全部頭名/值對的Map對象
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map headerValuesMap = externalContext
if (headerValuesMap!=null) {
Enumeration headers =
(Enumeration) headerValuesMap
while (headers
String value = (String) headers
System
}
}
getRequestCookies方法是HttpServletRequest
public abstract Cookie[] getRequestCookies()
例如
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Cookie[] cookies = externalContext
for (int i=
Cookie cookie = cookies[i];
String cookieName = cookie
String cookieValue = cookie
System
}
getRequestCookieMap方法返回一個包含當前請求中全部Cookie的
public abstract java
例如
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Map cookieMap = externalContext
if (cookieMap!=null) {
Cookie cookie = (Cookie) cookieMap
if (cookie!=null)
System
}
注意
Cookie名稱區分大小寫
getRequestLocale方法是ServletRequest
public abstract java
例如
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Locale locale = externalContext
System
System
getRequestContextPath是HttpServletRequest
public abstract String getRequestContextPath()
下面的代碼段把請求URI的上下文路徑輸出到控制台
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
System
externalContext
對於URL 來說
getRequestPathInfo方法是HttpServletRequest
public abstract String getRequestPathInfo()
例如
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
System
externalContext
對於URL 而言
getResourcePaths方法是ServletContext類的getResourcePaths方法的包裝器
public abstract java
比如
FacesContext facesContext = FacesContext
ExternalContext externalContext = facesContext
Set resourcePaths = externalContext
Iterator iterator = erator();
while (iterator
String path = (String) iterator
System
}
System
resourcePaths = externalContext
iterator = erator();
while (iterator
String path = (String) iterator
System
}
這段代碼兩次調用了getResourcePaths方法
/order
/index
/Styles
/images/
/details
/WEB
/checkOut
/browse
/shoppingCart
/search
/menu
第二個Set裡包含如下路徑
/WEB
/WEB
/WEB
/WEB
圖
getResourceAsStream方法是ServletContext
public abstract java
encodeURL方法是HttpServletResponse
public abstract String encodeURL(String url)
在portlet中使用JSF時
public abstract String encodeResourceURL(String sb)
encodeResourceURL方法迫使URL作為參數傳遞
public abstract String encodeResourceURL(String sb)
dispatchMessage方法可根據當前上下文分派請求
public abstract void dispatchMessage(String requestURL)
throws java
From:http://tw.wingwit.com/Article/program/Oracle/201311/17433.html