Java?ServletContext與ServletConfig接口使用教程
ServletContext接口
1.概念
當Servlet容器啟動時,會為每個Web應用創(chuàng)建一個唯一的ServletContext對象代表當前Web應用,可以和程序的容器(服務器)來通信。
兩種獲取方式:
通過request對象獲取
ServletContext context = request.getServletContext();
通過HttpServlet獲取
ServletContext context = this.getServletContext();
2.功能
1、獲取Web應用程序的初始化參數
定義了getInitParameterNames()和getInitParameter(String name)方法分別用來獲取參數名和參數值。
package cn.itcast.chapter04.servlet; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet03 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); // 得到ServletContext對象 ServletContext context = this.getServletContext(); // 得到包含所有初始化參數名的Enumeration對象 Enumeration<String> paramNames = context.getInitParameterNames(); // 遍歷所有的初始化參數名,得到相應的參數值,打印到控制臺 out.println("all the paramName and paramValue are following:"); // 遍歷所有的初始化參數名,得到相應的參數值并打印 while (paramNames.hasMoreElements()) { String name = paramNames.nextElement(); String value = context.getInitParameter(name); out.println(name + ": " + value); out.println("<br>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
2、實現多個Servlet對象共享數據
由于一個Web應用中的所有Servlet共享同一個ServletContext對象,因此ServletContext對象的域屬性可以被該Web應用中的所有Servlet訪問。在ServletContext接口中定義了分別用于增加、刪除、設置ServletContext域屬性的四個方法,如表1所示。
表1 ServletContext接口的方法
方法說明 | 功能描述 |
---|---|
Enumeration getAttributeNames() | 返回一個Enumeration對象,該對象包含了所有存放在ServletContext中的所有域屬性名 |
Object getAttibute(String name) | 根據參數指定的屬性名返回一個與之匹配的域屬性值 |
void removeAttribute(String name) | 根據參數指定的域屬性名,從ServletContext中刪除匹配的域屬性 |
void setAttribute(String name,Object obj) | 設置ServletContext的域屬性,其中name是域屬性名,obj是域屬性值 |
ServletContext context = this.getServletContext(); // 通過setAttribute()方法設置屬性值 context.setAttribute("data", "this servlet save data");
3、讀取Web應用下的資源文件
ServletContext接口中用于獲取資源路徑的相關方法,具體如下:
表2 ServletContext接口的常用方法
方法說明 | 功能描述 |
---|---|
Set getResourcePaths(String path) | 返回一個Set集合,集合中包含資源目錄中子目錄和文件的路徑名稱。參數path必須以正斜線(/)開始,指定匹配資源的部分路徑 |
String getRealPath(String path) | 返回資源文件在服務器文件系統(tǒng)上的真實路徑(文件的絕對路徑)。參數path代表資源文件的虛擬路徑,它應該以正斜線開始(/)開始,“/”表示當前Web應用的根目錄,如果Servlet容器不能將虛擬路徑轉換為文件系統(tǒng)的真實路徑,則返回null |
URL getResource(String path) | 返回映射到某個資源文件的URL對象。參數path必須以正斜線(/)開始,“/”表示當前Web應用的根目錄 |
InputStream getResourceAsStream(String path) | 返回映射到某個資源文件的InputStream輸入流對象。參數path傳遞規(guī)則和getResource()方法完全一致 |
編寫讀取itcast.properties資源文件:
package cn.itcast.chapter04.servlet; import java.io.*; import java.util.Properties; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet06 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); ServletContext context = this.getServletContext(); PrintWriter out = response.getWriter(); InputStream in = context .getResourceAsStream("/WEB- INF/classes/itcast.properties"); Properties pros = new Properties(); pros.load(in); out.println("Company=" + pros.getProperty("Company") + " <br>"); out.println("Address=" + pros.getProperty("Address") + " <br>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
ServletConfig接口
1.概念
Tomcat初始化一個Servlet時,會將該Servlet的配置信息封裝到一個ServletConfig對象中,通過調用init(ServletConfig cofig)方法將ServletConfig對象傳遞給Servlet。
2.ServletConfig的常用方法
表1 ServletConfig接口的常用方法
方法說明 | 功能描述 |
---|---|
String getInitParameter(String name) | 根據初始化參數名返回對應的初始化參數值 |
Enumeration getInitParameterNames() | 返回一個Enumeration對象,其中包含了所有的初始化參數名 |
ServletContext getServletContext() | 返回一個代表當前Web應用的ServletContext對象 |
String getServletName() | 返回Servlet的名字,即web.xml中<servlet-name>元素的值 |
package cn.itcast.chapter04.servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet02 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); // 獲得ServletConfig對象 ServletConfig config = this.getServletConfig(); // 獲得參數名為encoding對應的參數值 String param = config.getInitParameter("encoding"); out.println("encoding="+param); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
到此這篇關于Java ServletContext與ServletConfig接口使用教程的文章就介紹到這了,更多相關Java ServletContext與ServletConfig內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot的@ControllerAdvice處理全局異常詳解
這篇文章主要介紹了SpringBoot的@ControllerAdvice處理全局異常詳解,但有時卻往往會產生一些bug,這時候就破壞了返回數據的一致性,導致調用者無法解析,所以我們常常會定義一個全局的異常攔截器,需要的朋友可以參考下2024-01-01Java調用shell命令涉及管道、重定向時不生效問題及解決
這篇文章主要介紹了Java調用shell命令涉及管道、重定向時不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12java中l(wèi)ong(Long)與int(Integer)之間的轉換方式
這篇文章主要介紹了java中l(wèi)ong(Long)與int(Integer)之間的轉換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10