WEB容器在啓動時,它會爲每個WEB應用程序都創建一個對應的ServletContext對象,它代表當前web應用。
ServletConfig對象中維護了ServletContext對象的引用,開發人員在編寫servlet時,可以通過ServletConfig.getServletContext方法獲得ServletContext對象。
也可以使用 this.getServletContext方法
由於一個WEB應用中的所有Servlet共享同一個ServletContext對象,因此Servlet對象之間可以通過ServletContext對象來實現通訊。多個Servlet通過ServletContext對象實現數據共享。
ServletContext對象通常也被稱之爲context域對象。(request,session,page)
setAttribute(),getAttribute();
相關舉例:
實現Servlet的轉發。
RequestDispatcher rd = getServletContext().getRequestDispatcher(“/1.jsp”);
rd.forward(request,response);
如何把數據傳到 1.jsp ?(可以通過request域,不能用context域)
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/* 返回一個RequestDispatcher對象,作爲包裝器爲資源位於給定的路徑。
一個RequestDispatcher對象可用於轉發請求資源或包括資源在一個響應。資源可以是動態的還是靜態的。
路徑名必須開始以一個“/”和被解釋爲相對於當前上下文根。使用getContext獲取對資源RequestDispatcher在外國上下文。
該方法返回null如果ServletContext RequestDispatcher不能返回。*/
this.getServletContext().setAttribute("today","Yestoday");
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/index.jsp");
/* 從一個servlet將請求轉發到另一個資源(servlet、JSP文件,或HTML文件)在服務器。
這種方法允許一個servlet來做初步的請求處理和另一個資源生成響應。*/
rd.forward(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取ServletContext的兩種方式 //第一種 ServletContext sc=this.getServletConfig().getServletContext(); //第二種 ServletContext sc2=this.getServletContext(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //多個Servlet實現數據共享 String data="abcd"; this.getServletContext().setAttribute("lenovo", data); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ////配合Context2使用,通過屬性值lenovo調出data的值; String value = (String)this.getServletContext().getAttribute("lenovo"); System.out.println(value); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //在web.xml文件中查找data屬性相應的值; System.out.println(this.getServletContext().getInitParameter("data")); }
ServletContext應用
獲取WEB應用的初始化參數。
在web.xml文件中插入;
<context-param>
<param-name> data</param-name>
<param-value> xxxx</param-value>
</context-param>
ServletConfig和ServletContext的區別
整個Web應用只有一個ServletContext,在部署Web應用的時候,容器會建立這一個ServletContext對象,這個上下文對Web應用中的每個Servlet和JSP都可用。
Web應用中的各個Servlet都有自己的ServletConfig,它只對當前Servlet有效。
還有一個是通過一般的java類讀取配置文件的內容:
利用ServletContext對象讀取資源文件。
得到文件路徑
讀取資源文件的三種方式
.properties文件(屬性文件)
這個首先必須寫配置文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root
public void test1() throws IOException {
InputStream in= this.getServletContext().getResourceAsStream(
"/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(in);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
}
// 通過ServletContext讀取配置文件 方法2,獲取了文件的絕對路徑
public void test2() throws IOException {
String path = this.getServletContext().getRealPath(
"/WEB-INF/classes/db.properties");
int i = path.lastIndexOf("\\");
System.out.println(path.substring(i + 1));
FileInputStream fis = new FileInputStream(path);
Properties prop = new Properties();
prop.load(fis);
String driver = prop.getProperty("driver");
System.out.println(driver);
}
也可以再寫一個工具類:
InputStream in = StudentDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
然後再寫一個dao類,提供增刪改查;