ServletContext——被稱爲context域對象
1.servletContext域:1,是一個容器 2。作用範圍是應用程序範圍
服務器啓動時,他會爲每個WEB應用程序創建一個ServletContext對象,它代表當前web應用。停掉服務器時,ServletContext也就沒了。一個web應用中所有Servlet對象共享一個ServletContext,所以多個Servlet通過ServletContext對象實現數據共享
public class ServletDemo6 extends HttpServlet {
//servletContext示例
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到servletContext
ServletContext context=this.getServletConfig().getServletContext();——兩種方法
ServletContext context2=this.getServletContext();——兩種方法,這個比較好寫,用這個
String data=”aaaaa”;
context.setAttribute(“data”, data);
}
}
使用例子:比如可以用到聊天室
先寫一個Servlet
public class ServletDemo7 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data=”ddd”;
this.getServletContext.setAttribute(“data”,data);
}
}
再寫另一個Servlet
public class ServletDemo8 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = (String) this.getServletContext().getAttribute("data");
response.getOutputStream().write(value.getBytes());
}
}
2.還可以:這樣給整個web應用添一個參數
data
zzzz
data1
zzzz
調用:獲得web應用初始化參數
String value=this.getServletContext.getInitParameter(“data”);
還可以獲取所有,這裏就不寫了
3.還可以實現Servlet轉發
轉發:你找我借錢,我沒有,我幫你找別人。客戶機一次請求
重定向:你找我借錢,我沒有,我讓你自己去找別人。客戶機兩次請求
例子:
public class ServletDemo10 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data="aaaaaaaaaa";
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp");——得到轉發對象,即轉到哪裏去
rd.forward(request, response); ——實現轉發
}
}
4.ServletContext讀取web應用中的資源文件
public class ServletDemo11 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//test5();
File file=new File(“”);
response.getOutputStream().write(file.getAbsolutePath().getBytes());
}
方法一:這個方法比較好:這個方法可以得到當前讀取到資源的名稱
private void test5() throws IOException {
//得到絕對路徑
String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
//再通過傳統方法讀文件
String filename=path.substring(path.lastIndexOf(“\”));——這個方法可以得到當前讀取到資源的名稱
FileInputStream in2=new FileInputStream(path);
Properties props=new Properties();
props.load(in2);
System.out.println(props.getProperty(“url”));
}
方法二:
private void test4() throws IOException {
//in2的相對路徑是tomcat的bin目錄,所以這種方法要在該目錄下建立文件夾classes,並把文件放在這裏,所以不要用這個方法
FileInputStream in2=new FileInputStream(“classes/db.properties”);
Properties props=new Properties();
props.load(in2);
System.out.println(props.getProperty(“url”));
}
方法三:這個方法比較好:比較簡便,直接得到流,但這個方法不能得到當前讀取到資源的名稱
private void test3() throws IOException {
InputStream in=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");——得到資源文件位置
Properties props=new Properties();——一下代碼記住!!!對所有properties都這樣寫
props.load(in);——加載進來
System.out.println(props.getProperty("url"));
System.out.println(props.getProperty(“username”));
}
}