servletContext百科

servletContext
 
servletContext接口是Servlet中最大的一個接口,呈現了 web應用的Servlet視圖。ServletContext實例是經過 getServletContext()方法得到的,因爲HttpServlet繼承Servlet的關係GenericServlet類和HttpServlet類同時具備該方法。
 

1概要編輯

每一個應用都會有一個 ServletContext對象與之關聯,當容器分佈在在多個 虛擬機上時, web應用在所分佈的每一個虛擬機上都擁有一個 ServletContext實例.缺省狀況下, ServletContext不是分佈式的,而且只存在於一個虛擬機上。
經過ServletContext能夠訪問應用範圍的初始化參數和屬性

1).初始化參數

ServletContext對象是在Web應用程序裝載時初始化的。正像Servlet具備初始化參數同樣,ServletContext也有初始化參數。Servlet上下文初始化參數指定應用程序範圍內的信息。 [1]
web.xml 中配置初始化參數:
<context-param>
<param-name>adminEmail</param-name>
<param-value>webmaster</param-value>
</context-param>
<context-param>元素是針對整個應用的,因此並不嵌套在某個<servlet>元素中,該元素是<web-app>元 素的直接子元素。 [1]
從Servlet中訪問初始化參數:
ServletContext application=this.getServletContext();
out.println("send us your")
out.println(application.getInitParameter("email"));
out.println("'>email");

2).屬性

能夠經過編程的方式綁定,也能夠做爲 web應用的 全局變量被全部Servlet和JSPs訪問
設置Context屬性:
ServletContext application=this.getServletContext();
application.setAttribute("person1",new Person("Jim"));
application.setAttribute("person2",new Person("Green"));
獲取Context屬性:
ServletContext application=this.getServletContext();
Enumberation persons=application.getAttributeNames();
while(persons.hasMoreElements()){
String name=(String)persons.nextElement();
Person p=(Person)persons.getAttribute(name);
application.removeAttribute(name);

2ServletContext的用途編輯

安裝方法:

安裝在一個服務器中的一個特定URL名字空間(好比,/myapplication)下的全部Servlet,JSP,JavaBean等Web部件的集合構成了一個Web的應用,每個Web應用(同一JVM),容器都會有一個背景對象,而javax.servlet.ServletContext接口就提供了訪問這個背景對象的途徑。
Servlet實例的getServletContext方法:
獲得該Servlet運行其中的這個背景對象。從這個背景對象中你能夠訪問以下信息或資源:(注意該方法不是ServletContext的方法而是獲取背景對象的方法因爲HttpServlet繼承Servlet的關係GenericServlet類和HttpServlet類同時具備該方法):初始化參數 ServletContext.getInitParameter(String name)。 存儲在背境中的對象 context.getAttribute(String name) 與本背景關聯的資源 ServletContext.getResource(String path)  日誌 ServletContext.log(String msg) 以上所示方法均爲ServletContext所提供,值得一提的是對於存儲在背境中的對象訪問方法經常使用的還有: context.setAttribute(String name, Object object);將特定名字綁定的任意類型的對象上。將把object對象綁定到名字name,存放在Servlet背景中,可供同一背景中的其餘Servlet共享。其餘Servlet能夠經過context.getAttribute(String name),獲得一個背景中的對象,或經過context.removeAttribute(String name)在背景中移除一個對象。

在Web應用範圍內存取共享數據的方法:

setAttribute(String name,java.lang.Objectobject):把一個java 對象和一個屬性名綁定,並存放到ServletContext 中,參數name 指定屬性名,參數Object 表示共享數據。
getAttribute(String name):根據參數給定的屬性名,返回一個Object類型的對象。
getAttributeNames():返回一個Enumeration 對象,該對象包含了全部存放在ServletContext 中的屬性名
removeAttribute(String name) : 根 據 參 數 指 定 的 屬 性 名 , 從servletContext 對象中刪除匹配的屬性。
getRealPath("/"):獲得絕對路徑

訪問web應用的靜態資源

使用ServletContext接口能夠直接訪問 web應用中的 靜態內容文檔結構.包括HTML,GIF和JPEG文件。如如下方法:
.getResource
.getResourceAsStream
這兩個方法的參數都是以"/"開頭的字符串,表示資源相對於context根的 相對路徑.文檔結構能夠存在於服務器文件系統,或是war包中,或是在遠程服務器上,抑或其餘位置.不能夠用來得到動態資源,好比,getResource("/index.jsp"),這個方法將返回該jsp文件的 源碼,而不是 動態頁面.能夠用"Dispatching Requests"得到動態內容.
列出 web應用中可被訪問的資源,可使用getResourcePaths(String path)方法。

跨多個請求,用戶和Servlets

web服務器支持在一臺機器上共享一個IP的多個邏輯 主機,這種能力被稱爲"虛擬主機",每一個邏輯主機都擁有它本身的servlet context。servlet context不能跨 虛擬主機共享。
相關文章
相關標籤/搜索