ServletConfig
ServeltConfig类
从名字上来看,ServletConfig是我们Servlet程序的配置信息类。
Servlet程序和ServletConfig类都是由我们Tomcat负责创建,我们负责使用。
Servlet程序与ServletConfig类的生命周期
-
Servlet程序默认是第一次访问的时候创建
-
ServletConfig类是每个Servlet程序创建时,就会创建一个对应的ServletConfig对象。
ServletConfig类的作用
- 可以获取Servlet程序的别名
servlet-name
的值 - 获取初始值参数
init-param
- 获取
ServletContext
对象
作用测试:
package com.servlet.test;
/**
* @author LHN
* @create 2021-10-18 14:32
*/
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.IOException;
public class servletconfigtest1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取servlet程序的别名
System.out.println("该servlet程序的别名为 " + getServletConfig().getServletName());
//获取servlet的初始化值(是在web.xml中的对应servlet程序配置中添加的init-param)
System.out.println("该servlet程序的初始化值" + getServletConfig().getInitParameter("usename"));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>servletconfigtest1</servlet-name>
<servlet-class>com.servlet.test.servletconfigtest1</servlet-class>
<!-- 初始化参数 -->
<init-param>
<param-name>usename</param-name>
<param-value>servletconfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletconfigtest1</servlet-name>
<url-pattern>/servletconfig</url-pattern>
</servlet-mapping>
</web-app>
第三个作用是获取ServletContext类
。那么什么是ServletContext
ServletContext
-
ServletContext是一个接口,他表示Servlet上下文对象
-
一个web工程,只有一个
ServletContext
对象实例 -
ServletContex
t是一个域对象 -
ServletContext
是在web工程部署的时候创建,在web工程停止的时候消亡。
域对象:域对象是可以如Map一样存取数据的对象,这里的域指的是存取数据的范围,而ServletContext的范围是整个web工程
通常情况下,因为域对象是一个用来存取数据的对象,所以就有一些用来存取数据的方法,与Map中的一些方法一样的作用
存数据 | 取数据 | 删除数据 | |
---|---|---|---|
Map | put() | get() | remove() |
域对象 | setAttribute() | getAttribute() | removeAttribute() |
ServletContext的四个作用
- 获取web.xml配置文件中上下文参数
context-param
- 获取当前工程路径 , 格式:/工程路径
- 获取工程部署后在服务器硬盘上的绝对路径
- 像Map一样存取数据
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.IOException;
public class servletconfigtest1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过ServletConfig类获取ServletContext的实例对象。
ServletContext servletContext = getServletConfig().getServletContext();
//通过getInitParameter(String s)来获取Context-Param中定义的上下文参数
System.out.println(servletContext.getInitParameter("key1"));
System.out.println(servletContext.getInitParameter("key2"));
//获取当前工程路径 / 斜杠被服务器解析为:http://ip:port/工程名/
System.out.println(servletContext.getContextPath());//结果 /servletconfig
//获取工程部署后在服务器硬盘上的绝对路径
System.out.println(servletContext.getRealPath("servletconfigtest1"));
//像Map一样存取数据
servletContext.setAttribute("key3","value3");
System.out.println(servletContext.getAttribute("key3"));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
web.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>key2</param-name>
<param-value>value2</param-value>
</context-param>
<context-param>
<param-name>key1</param-name>
<param-value>value1</param-value>
</context-param>
<servlet>
<servlet-name>servletconfigtest1</servlet-name>
<servlet-class>com.servlet.test.servletconfigtest1</servlet-class>
<!-- 初始化参数 -->
<init-param>
<param-name>usename</param-name>
<param-value>servletconfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>servletconfigtest1</servlet-name>
<url-pattern>/servletconfig</url-pattern>
</servlet-mapping>
</web-app>