研究javax.servlet.ServletContext接口
1、javax.servlet.ServletContext是一個接口
2、javax.servlet.ServletContext是Servlet規範中的一員
3、apache Tomcat 伺服器對ServletContext接口的實作類完整類名:
org.apache.catalina.core.ApplicationContextFacade
package org.apache.catalina.core;
public class ApplicationContextFacade implements javax.servlet.ServletContext{
}
程式員不需要關心實作類,還是面向ServletContext接口程式設計。
4、ServletContext是什麼?
* ServletContext對象被翻譯為“Servlet對象的上下文對象”。
* Servlet上下文是所有的Servlet對象共享的一個四周環境對象。
5、在同一個webapp中,所有的Servlet對象共享一個ServletContext對象。
6、ServletContext對象在web伺服器啟動階段被建立,在web伺服器關閉的時候被銷毀,生命周期最長的一個對象。
ServletContext對象在web伺服器啟動階段解析web.xml檔案的時候被建立。
7、一個webapp對應一個ServletContext對象。
8、ServletContext在開發中能幹什麼?
* ServletContext對象在webapp中隻有一個。所有Servlet對象共享,也可以看做是所有使用者共享的一個對象。
* ServletContext對象中适合存放什麼資料?
- 所有使用者共享的資料(不是使用者共享的資料不适合放到一個共享的環境當中)
- 少的資料量(資料量龐大會占用大量的記憶體空間,影響效率)
- 不被修改的資料(經常修改的資料不适合放到該環境中,因為有線程安全隐患)
注意:當以上的條件全部滿足之後,資料可以存放到該環境當中,這個環境本身就是一種緩存機制, 沒有必要每一次都去連接配接資料庫,連接配接資料庫是需要頻繁的IO操作的,減少IO操作需要使用緩存解決方案,可以大大提高程式的執行效率。
9、ServletContext通常變量名叫做:application
ServletContext application;
application翻譯為:應用
一個應用隻有一個ServletContext對象。
ServletContext對象是一個應用級别的對象。
10、ServletContext接口中常用的方法:
-void setAttribute(String name, Object object) //map.put(key,value);
-Object getAttribute(String name) //Object value = map.get(key);
-void removeAttribute(String name) //map.remove(key);
-Enumeration getInitParameterNames() 擷取上下文初始化參數的name
-String getInitParameter(String name) 通過上下文初始化參數的name擷取value
<!-- 上下文初始化參數 -->
<!-- 以下的配置資訊被自動封裝到ServletContext對象當中 -->
<!-- 全局配置,這裡的配置資訊,在所有的Servlet中都可以通路 -->
<context-param>
<param-name>user</param-name>
<param-value>zhangsan</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123</param-value>
</context-param>
-String getRealPath(String path) 擷取檔案的絕對路徑
ServletContextDemo代碼如下:
package com.servletConfig;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletContextDemo implements Servlet {
//執行個體變量
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
//将局部變量config指派給執行個體變量config
this.config = config;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
//擷取ServletContext對象
ServletContext application = config.getServletContext();
//[email protected]
System.out.println("application = " + application);
//擷取上下文初始化參數配置資訊
Enumeration<String> contextNames = application.getInitParameterNames();
while(contextNames.hasMoreElements()){
String name = contextNames.nextElement();
String value = application.getInitParameter(name);
System.out.println("ServletContext======" + name + " = " + value);
}
//擷取檔案絕對路徑
//注意:路徑在寫的時候不需要以“/”開始,但是從WebRoot檔案夾下出發
String realPath = application.getRealPath("index.html");
System.out.println(realPath); //D:\tomcat7\tomcat7\webapps\hello\index.html
//向ServletContext對象中或者說向application範圍中綁定一個資料,并起名
application.setAttribute("fdsafdsaf", "jojio");
// 【注:另一個Servlet執行個體】 從應用範圍當中或者說從ServletContext範圍當中通過name讀取某個綁定的資料
Object obj = application.getAttribute("fdsafdsaf");
//将取出的對象輸出到浏覽器中
response.getWriter().print(obj.toString());
}
@Override
public void destroy() {
}
//子類使用該方法可以擷取到ServletConfig
@Override
public ServletConfig getServletConfig() {
return config;
}
@Override
public String getServletInfo() {
return null;
}
}
web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 上下文初始化參數 -->
<!-- 以下的配置資訊被自動封裝到ServletContext對象當中 -->
<!-- 全局配置,這裡的配置資訊,在所有的Servlet中都可以通路 -->
<context-param>
<param-name>user</param-name>
<param-value>admin</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>112233</param-value>
</context-param>
<servlet>
<servlet-name>b</servlet-name>
<servlet-class>com.servletConfig.ServletContextDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/b</url-pattern>
</servlet-mapping>
</web-app>
希望對你有幫助,祝你有一個好心情,加油!
若有錯誤、不全、可優化的點,歡迎糾正與補充!