JavaWeb ServletContext基礎(chǔ)與應(yīng)用詳細講解
ServletContext 基礎(chǔ)知識
獲取 ServletContext對象
有兩種方式可以獲?。?/p>
- 使用 servletconfig 對象獲取
- 使用 servlet 上下文獲取
// 第一種方式
ServletContext app1 = config.getServletContext();
writer.println("<br>" + app1);
// 第二種方式
ServletContext app2 = this.getServletContext();
writer.println("<br>" + app2);
特性
- 一個 webapp 只存在一個 ServletContext 對象
- ServletContext 對象是由 web 服務(wù)器啟動時創(chuàng)建
- ServletContext 是一個接口,他依然遵循 servlet 規(guī)范
- ServletContext 在 web 服務(wù)器銷毀時才會銷毀
- 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 內(nèi)共享的配置信息,而 servlet-name 更適用于僅供單個 servlet 使用
獲取文件路徑
獲取根路徑,即我們配置 web 服務(wù)器時設(shè)置的路徑
// 獲取web的根路徑
String contextPath = servletContext.getContextPath();
writer.println("<br>" + contextPath);
獲取指定文件的絕對路徑;
getRealPath 接收一個路徑作為參數(shù),這個路徑的起始點是 web 文件夾,下方代碼指的是 web/index.html 這個文件
注意注意!getRealPath 的參數(shù)別帶上根目錄路徑?。。?/p>
// 獲取絕對路徑
String realPath = servletContext.getRealPath("/index.html");
writer.println("<br>" + realPath);
記錄日志
可以使用 log 方法記錄日志;
日志保存位置為 tomcat 根目錄下/logs
servletContext.log("hellowrodl");
參數(shù)增刪改查
我們可以直接在 servlet 中直接對 context-param 進行增添、查找、刪除?。。?/p>
// 以鍵值對的方式添加參數(shù)
servletContext.setAttribute("name","tom");
// 獲取參數(shù)
Object name = servletContext.getAttribute("name");
// 刪除參數(shù)
servletContext.removeAttribute("name");
到此這篇關(guān)于JavaWeb ServletContext基礎(chǔ)與應(yīng)用詳細講解的文章就介紹到這了,更多相關(guān)JavaWeb ServletContext內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java構(gòu)造器(構(gòu)造方法)與方法區(qū)別說明
這篇文章主要介紹了Java構(gòu)造器(構(gòu)造方法)與方法區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Java實現(xiàn)IP地址到二進制的轉(zhuǎn)換
這篇文章主要為大家詳細介紹了Java實現(xiàn)IP地址到二進制的轉(zhuǎn)換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

