JavaWeb ServletContext基礎與應用詳細講解
ServletContext 基礎知識
獲取 ServletContext對象
有兩種方式可以獲?。?/p>
- 使用 servletconfig 對象獲取
- 使用 servlet 上下文獲取
// 第一種方式
ServletContext app1 = config.getServletContext();
writer.println("<br>" + app1);
// 第二種方式
ServletContext app2 = this.getServletContext();
writer.println("<br>" + app2);
特性
- 一個 webapp 只存在一個 ServletContext 對象
- ServletContext 對象是由 web 服務器啟動時創(chuàng)建
- ServletContext 是一個接口,他依然遵循 servlet 規(guī)范
- ServletContext 在 web 服務器銷毀時才會銷毀
- ServletContext 存儲了 整個 web.xml 的信息
context-param
和 servletconfig 一樣,他也可以獲取與 web.xml 中定義的參數(shù);
只不過 ServletContext 定義的參數(shù)是寫在 servlet 標簽外面的;
每一個 context-param 都代表一個 key-value;
如果需要多個參數(shù)就必須要 分別寫 context-param
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<!-- 定義了兩個外部參數(shù) -->
<context-param>
<param-name>pageSize</param-name>
<param-value>10</param-value>
</context-param>
<context-param>
<param-name>porkPrice</param-name>
<param-value>999</param-value>
</context-param>
...
</web-app>
同樣的,我們可以使用 Enumeration 獲取所有的 context-param,然后再使用迭代器的方法一次獲取其中的 key 和 value
package com.zhiyiyi.javaweb.servlet;
import jakarta.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
public class AServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
servletResponse.setContentType("text/html");
PrintWriter writer = servletResponse.getWriter();
ServletContext servletContext = this.getServletContext();
// 迭代輸出param-name和param-value
Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
while (initParameterNames.hasMoreElements()) {
String key = initParameterNames.nextElement();
String value = servletContext.getInitParameter(key);
writer.println(key + " " + value + "<br>");
}
}
}
可見,context-param 定義的是 webapp 內共享的配置信息,而 servlet-name 更適用于僅供單個 servlet 使用
獲取文件路徑
獲取根路徑,即我們配置 web 服務器時設置的路徑
// 獲取web的根路徑
String contextPath = servletContext.getContextPath();
writer.println("<br>" + contextPath);
獲取指定文件的絕對路徑;
getRealPath 接收一個路徑作為參數(shù),這個路徑的起始點是 web 文件夾,下方代碼指的是 web/index.html 這個文件
注意注意!getRealPath 的參數(shù)別帶上根目錄路徑!??!
// 獲取絕對路徑
String realPath = servletContext.getRealPath("/index.html");
writer.println("<br>" + realPath);
記錄日志
可以使用 log 方法記錄日志;
日志保存位置為 tomcat 根目錄下/logs
servletContext.log("hellowrodl");
參數(shù)增刪改查
我們可以直接在 servlet 中直接對 context-param 進行增添、查找、刪除!??!
// 以鍵值對的方式添加參數(shù)
servletContext.setAttribute("name","tom");
// 獲取參數(shù)
Object name = servletContext.getAttribute("name");
// 刪除參數(shù)
servletContext.removeAttribute("name");
到此這篇關于JavaWeb ServletContext基礎與應用詳細講解的文章就介紹到這了,更多相關JavaWeb ServletContext內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Mybatis分頁插件PageHelper配置及使用方法詳解
這篇文章主要介紹了Mybatis分頁插件PageHelper配置及使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
IDEA連接postgressql數(shù)據(jù)庫操作
這篇文章主要介紹了IDEA連接postgressql數(shù)據(jù)庫操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
AsyncHttpClient?ClientStats源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient?ClientStats源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12

