2、ServletContext詳解
ServletContext是servlet與servlet容器之間的直接通訊的接口。Servlet容器在啓動一個Webapp時,會爲它建立一個ServletContext對象,即servlet上下文環境。每一個webapp都有惟一的ServletContext對象。同一個webapp的全部servlet對象共享一個ServeltContext,servlet對象能夠經過ServletContext來訪問容器中的各類資源。
ServletContext接口提供的方法分爲如下幾種類型:
用於在Webapp範圍內存取共享數據的方法。
注:webapp範圍具備如下兩層含義:
(1) 表示有webapp的生命週期構成的時間段。
(2) 表示在webapp的生命週期內全部web組件的集合。
◆ setAttribute(String name, java.lang.Object object):以鍵值對的形式,把一個java對象和一個屬性名綁定,並存放到ServletContext中,參數name指定屬性名,參數Object表示共享數據。
◆ getAttribute(String name):根據參數給定的屬性名,返回一個Object類型的對象。
◆ getAttributeNames():返回一個Enumeration對象,該對象包含了全部存放在ServletContext中的屬性名。
◆ removeAttribute(String name):根據參數指定的屬性名,從servletContext對象中刪除匹配的屬性。
訪問當前Webapp的資源
◆ getContextpath():返回當前webapp的URL入口。
◆ getInitParameter(String name):返回webapp配置文件中匹配的初始化參數值。在web.xml中<web-app>元素中<context-param>子元素表示Webapp應用範圍內的初始參數。
◆ getInitParameterNames():返回一個Enumeration對象,該對象包含了全部存放在web.xml中<web-app>元素中<context-param>子元素的初始化參數名。
◆ getServletContextName():返回webapp名稱。即<web-app>元素中<display-name>子元素的值。
◆ getRequestDispatcher(String path):返回一個用於向其餘web組件轉發請求的RequestDispatcher對象。
訪問servlet容器的相關信息
◆ getContext(String uripath):根據參數指定的url,返回當前servlet容器中其餘web應用的servletContext()對象。
訪問web容器的相關信息
◆ getMajorVersion():返回servlet容器支持的java servlet API 的主版本號。
◆ getMinorVersion():返回servlet容器支持的java Servlet API的次版本號。
◆ getServerInfo():返回servlet容器的名字和版本。
訪問服務器端的文件系統資源
◆ getRealPath(String path):根據參數指定的虛擬路徑,返回文件系統中的一個真實的路徑。
◆ getResource(String path):返回一個映射到參數指定的路徑的url。
◆ getResourceAsStream(String path):返回一個用於讀取參數指定的文件的輸入流。(把文件讀到輸入流中去)
◆ getMimeType(String file):返回參數指定的文件的MIME類型。
輸出日誌
◆ log(String msg):向servlet的日誌文件中寫日誌。
◆ log(String message, java.lang.Throwable throwable):向servlet的日誌文件中寫錯誤日誌,以及異常的堆棧信息。
ServletContext對象得到幾種方式:
javax.servlet.http.HttpSession.getServletContext()
javax.servlet.jsp.PageContext.getServletContext()
javax.servlet.ServletConfig.getServletContext()
以上是servlet2.5版本及之前的獲取方法。
servlet3.0中新增方法:
javax.servlet.ServletRequest.getServletContext()
3、ServletConfig詳解
Jsp/Servlet容器初始化一個Servlet類型的對象時,會爲這個Servlet對象建立一個ServletConfig對象。在ServletConfig對象中包含了Servlet的初始化參數信息。此外,ServletConfig對象還與ServletContext對象關聯。Jsp/Servlet容器在調用Servlet對象的init(ServletConfig config)方法時,會把ServletConfig類型的對象當作參數傳遞給servlet對象。Init(ServletConfig config)方法會使得當前servlet對象與ServletConfig類型的對象創建關聯關係。
ServletConfig接口中定義瞭如下方法:
◆ getInitParameter(String name):根據給定的初始化參數,返回匹配的初始化參數值。
◆ getInitParameterNames():返回一個Enumeration對象,該對象包含了全部存放在web.xml中<web-app>元素<servlet>子元素<init-param>中的全部的初始化參數名。
◆ getServletContext():返回一個servletContext()對象,
◆ getServltName():返回servlet的名字,即web.xml中的相對應的servlet的子元素<servlet-name>的值。若是沒有配置這個子元素,則返回servlet類的全侷限定名。
Example:
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>first</param-name>
<param-value>netfish</param-value>
</init-param>
<init-param>
<param-name>last</param-name>
<param-value>blog</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
如上,紅色標記處爲初始化參數,至關於servlet共享參數。
在servlet中:
public class BlogServlet extends HttpServlet {
String first = this.getInitParameter("first");
String last = this.getInitParameter("last");
System.out.println("第一個參數:" + first + "第二個參數:" + last);
}
注:HttpServlet類繼承了GenericServlet類,而GenericServlet類實現了ServletConfig接口,所以在HttpServlet類和GenericServlet類及子類中均可以直接調用ServletConfig接口中的方法。
4、ServletContext和ServletConfig兩者區別
從做用範圍來講,ServletConfig做用於某個特定的Servlet,即從該Servlet實例化,那麼就開始有效,可是該Servlet以外的其餘Servlet不能訪問;ServletContext做用於某個webapp,即在一個webapp中至關於一個全局對象,在Servlet容器啓動時就已經加載,對於不一樣的webapp,有不一樣的ServletContext。
其次,來看一下兩者參數的使用。若是一個參數爲整個webapp所用,那麼就配置爲ServletContext參數,以下所示:
<context-param>
<param-name>ContextParam</param-name>
<param-value>hello, this is ServletContext param.</param-value>
</context-param>
若是一個參數僅爲一個Servlet所用,那麼就應該配置爲ServletConfig參數,以下所示:
<servlet>
<servlet-name>servlet名稱</servlet-name>
<servlet-class>servlet全侷限定名</servlet-class>
<init-param>
<param-name>ServletParam</param-name>
<param-value>hello, this is ServletConfig param.</param-value>
</init-param>
</servlet>
最後,說明一下參數的獲取。訪問ServletConfig參數,取得ServletConfig對象後,調用getInitParameter()方法;訪問ServletContext對象,只要調用現有的ServletConfig對象的getServletContext()便可,而後一樣調用getInitParamter()方法就能獲取參數。例如對於上面的參數,能夠經過以下方法獲取各自參數。
public class GetParam extends HttpServlet {
ServletConfig config;
public void init(ServletConfig config) {
this.config=config;
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
String servletparam=(String)config.getInitParameter("ServletParam");
System.out.println(servletparam);
String contextparam = (String)config.getServletContext().getInitParameter("ContextParam");
System.out.println(contextparam);
}
public void destroy() {
}
}
對上面Servlet編譯後,部署到Tomcat中,啓動,在瀏覽器中輸入http://localhost:8080/webapp名稱/GetParam(該連接地址在webx.xml中配置)訪問,就能夠分別在控制檯輸出hello, this is ServletConfig param.
hello, this is ServletContext param.
轉載來自http://blog.csdn.net/u012077981/article/details/17137705