05-ServletconfigServletCOntext
一、 ServletConfig 對象
作用 : 封裝 Servlet 初始化參數
1. 可以在 web.xml 文件中 Servlet 元素下 為Servlet配置初始化參數
<init-param>
??? ??? <param-name>name</param-name>
??? ??? <param-value>aaaa</param-value>
</init-param>
2. web 容器在初始化Servlet時,會將初始化參數封裝到一個 ServletConfig 對象中,傳給init方法
?
3. 我們在Servlet 中覆寫 init方法,就可以獲得ServletConfig
?
4. 父類 GenericServlet 中定義了一個成員變量用于記住此對象,并提供了 getServletConfig 方法
我們可以直接調用此方法 獲得 config 對象
?
5. 再調用 getInitParameter(name) 方法獲得想要配置項
// 指定編碼
?????? // 獲得ServletConfig 對象
?????? ServletConfig config = getServletConfig();
?????? String encoding = config.getInitParameter("encoding");
??????
??????
?????? System.out.println("encoding=" + encoding);
?
二、 ServletContext 對象
1. ServletContext對象代表整個web應用
2. ServletContext對象是一個域對象(可以存儲數據的對象)
ServletContext對象的內部維護了一個map集合, key是String類型 value是Object類型
class ServletContext {
??? private Map<String, Object> map ;
}
?
3. ServletContext 作為域對象, 多個Servlet 可以共享數據
Servlet6
// 1. 獲得ServletContext 對象
ServletContext context = getServletContext();
// 2. 存入域
context.setAttribute(“name”, “zhangsan”);
?
Servlet7
// 獲得 context 域, getAttribute
String name = (String) getServletContext().getAttribute("name");
?
4.獲取web應用的初始化參數
??? getContext().getInitParameter(“name”);
?
5. 統計一個web應用的訪問量
?? 在 context 域中維護一個count變量
?? 訪問Servlet時,取出變量加1
?
6. 實現請求轉發
實現請求轉發需要用到轉發對象 RequestDispatcher
有一個 forward 方法能轉發請求
?
7. 如何讀取工程中的文件
7.1.? 讀取web工程下的資源文件
?????? // 獲得絕對路徑
????? String realPath = ServletContext.getRealPath(相對web應用的路徑);
??? 注意URL url =? ServletContext.getResource();? web的url
?????? // 獲得與文件關聯的流
?????? InputStream in=
?ServletContext.getResourceAsStream(“WEB-INF/classes/config.properties”;
?
7.2?? 讀取java工程下的文件
?? // 不能相對虛擬機目錄不能用絕對路徑
?? // 只能類加載的方式讀
??? // 獲得 流
??? ClassLoader classLoader = Demo.class.getClassLoader();?
??????
??? InputStream in = classLoader.getResourceAsStream("a.txt");
?
??? // 獲得絕對路徑
??? URL url = Demo.class.getClassLoader().getResource("a.txt");
?
??? String path = url.getPath();
?
類加載方式缺點
1)不能讀取類路徑以外文件
2) ?由于需要加載到內存,不能讀大文件
?
web工程中如果用類加載的方式讀
類加載實際上讀取的是內存中加載的文件,此時將讀不到硬盤上資源文件的修改
解決辦法Demo.class.getClassLoader().getResource("a.txt").getPath()
通過絕對路徑去讀硬盤上的文件? 避開內存的文件
三、 Servlet緩存
??? HttpServlet 的 Service方法中的代碼
?????? // 調用方法
long lastModified = getLastModified(req);
?????? // 如果為 -1 ,就直接放行,給最新的
??????????? if (lastModified == -1) {
??????????????? doGet(req, resp);
}
// 方法返回不是-1
?else {
??? // 讀取IE發送的頭If-Modified-Since
??????????????? long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
????????????? // 拿著客戶端的時間頭和方法的返回值比較
??????????????? if (ifModifiedSince < (lastModified / 1000 * 1000)) {
??????????????????? // If the servlet mod time is later, call doGet()
??????????????????? // Round down to the nearest second for a proper compare
??????????????????? // A ifModifiedSince of -1 will always be less
????????????????? // 方法的返回值大于ie發送過來的時間頭
????????????????? // 重新向瀏覽器發送了一個時間頭
??????????????????? maybeSetLastModified(resp, lastModified);
????????????????? // 放行, 發送頁面
??????????????????? doGet(req, resp);
??????????????? } else {
????????????????? // 方法的返回值沒有大于ie發送過來的時間頭
????????????????? // 發送 304 狀態碼,讓用戶去讀緩存
??????????????????? resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
??????????????? }
??????????? }
?
?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的05-ServletconfigServletCOntext的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 04-Servlet入门+http协议
- 下一篇: 采用常规动力的航母“003”