JavaWeb學習筆記:ServletConfig()和ServletContext()
ServletConfig()和ServletContext()
1.ServletConfig()
ServletConfig是一個接口,它由server提供商來實現。
ServletConfig封裝了Servlet的配置信息,而且能夠獲取ServletContext對象。
Servlet容器在初始化servlet的時候會初始化一個servletConfig對象,這個對象在不論什麽可訪問的client都是有效的。可是,需註意的是,該對象僅僅能在本servlet中應用,不能在其它servlet中訪問。
基本的方法:
- public ServletContext getServletContext(); //暫不寫,放在ServletContext中整理
- public String getInitParameter(String name); //獲取指定參數名的初始化參數
- public Enumeration< String > getInitParameterNames(); //獲取參數名組成的 Enumeration 對象
- public String getServletName(); //使用比較少
獲取配置信息
怎樣配置
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.buaa.zhao.HelloServlet</servlet-class>
<!-- 配置 Serlvet 的初始化參數。 且節點必須在 load-on-startup 節點的前面 -->
<init-param>
<!-- 參數名 -->
<param-name>user</param-name >
!-- 參數值 -->
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>12345</param-value>
</init-param>
<load-on-startup>-1</load-on-startup>
</servlet>
獲取配置信息
public void init(ServletConfig servletConfig) throws ServletException {
String user = servletConfig.getInitParameter("user");
//輸出root
System.out.println(user);
System.out.println("--------------------------");
Enumeration<String> names = servletConfig.getInitParameterNames();
//輸出:
//user: root
//password: 12345
while(names.hasMoreElements()){
String name = names.nextElement();
String value = servletConfig.getInitParameter(name);
System.out.println(name + ": " + value);
}
}
2.ServletContext()
能夠覺得 SerlvetContext 是當前 WEB 應用的一個大管家. 能夠從中獲取到當前 WEB 應用的各個方面的信息
基本的方法
- public String getInitParameter(String name); //獲取指定參數名的初始化參數
- public Enumeration< String > getInitParameterNames(); //獲取參數名組成的 Enumeration 對象
- public String getRealPath(String path);
- public String getContextPath();
- public InputStream getResourceAsStream(String path);
獲取配置信息
ServletContext實例能夠通過 serlvetConfig.getServletContext()方法獲得的.
該對象代表當前 WEB 應用: 能夠覺得 SerlvetContext 是當前 WEB 應用的一個大管家. 能夠從中獲取到當前 WEB 應用的各個方面的信息。
怎樣配置
<!-- 配置當前 WEB 應用的初始化參數 -->
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:localhost:///test</param-value>
</context-param>
獲取配置信息
public void init(ServletConfig servletConfig) throws ServletException {
//獲取ServletContext對象
ServletContext servletContext = servletConfig.getServletContext();
String driver = servletContext.getInitParameter("driver");
//輸出:driver: com.mysql.jdbc.Driver
System.out.println("driver: " + driver);
Enumeration<String> nameContext = servletContext.getInitParameterNames();
//輸出
//name: driver
//name: jdbcUrl
while(nameContext.hasMoreElements()) {
String name = nameContext.nextElement();
System.out.println("name: " + name);
}
}
獲取當前Web應用的某個文件部署在在server上的絕對路徑
須要通過方法public String getRealPath(String path);
來獲取。
代碼:
String realPath = servletContext.getRealPath("/zhaoTest");
System.out.println(realPath);
輸出結果:
D:\workspace\sts\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\***\zhaoTest.xml
獲取當前 WEB 應用的名稱
須要通過方法getContextPath();
來獲取。
代碼:
String contextPath = servletContext.getContextPath();
System.out.println(contextPath);
獲取當前web應用的而某一個文件相應的輸入流
通過getResourceAsStream(String path)
獲取。path 的 / 為當前 WEB 應用的根文件夾。
有兩種方法能夠獲取到。
代碼
try {
ClassLoader classLoader = getClass().getClassLoader();
InputStream is = classLoader.getResourceAsStream("jdbc.properties");
System.out.println("1. " + is);
pros.load(is);
System.out.println(pros.getProperty("name"));
} catch (Exception e) {
e.printStackTrace();
}
pros = new Properties();
try {
//註意: / 為當前 WEB 應用的根文件夾。
InputStream is2 = servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
System.out.println("2. " + is2);
pros.load(is2);
System.out.println(pros.getProperty("name"));
} catch (Exception e) {
e.printStackTrace();
}
結果
1. java.io.BufferedInputStream@15d442ac
2. java.io.FileInputStream@22653566
ServletConfig()和ServletContext()的差別
- 作用範圍不同:ServletContext的參數能夠為全部的Servlet所獲取,ServletConfig僅僅能由當前Servlet所獲取。
也就是說他們的差別在於,所填寫的配置的信息是想讓一個Servlet用還是多個Servlet用。
- 配置不同:ServletContext是在兩個元素間配置,ServletConfig是在元素之間的間配置。
JavaWeb學習筆記:ServletConfig()和ServletContext()