ServletConfig配置Servlet信息
ServletConfig接口是Tomcat服务器提供的org.apache.catalina.core.StandardWrapperFacade给实现的
ServletConfig被翻译为Servlet对象的配置信息对象是Servlet规范中的一员,全类名jakarta.servlet.ServletConfig
<servlet>
<servlet-name>configTestservlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.ConfigTestServletservlet-class>
<init-param>
<param-name>driverparam-name>
<param-value>com.mysql.cj.jdbc.Driverparam-value>
init-param>
servlet>
ServletConfig对象的创建时机和使用时机
Servlet和ServletConfig一一对应
在web.xml文件中配置两个Servlet对象
<servlet>
<servlet-name>configTestservlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.ConfigTestServletservlet-class>
<init-param>
<param-name>driverparam-name>
<param-value>com.mysql.cj.jdbc.Driverparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>configTestservlet-name>
<url-pattern>/testurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>configTest2servlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.ConfigTestServlet2servlet-class>
<init-param>
<param-name>keyparam-name>
<param-value>valueparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>configTest2servlet-name>
<url-pattern>/test2url-pattern>
servlet-mapping>
Servlet对象和ServletConfig对象是一一对应的关系
public class ConfigTestServlet extends GenericServlet {
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 获取ServletConfig对象
ServletConfig config = this.getServletConfig();
// 输出ServletConfig对象: org.apache.catalina.core.StandardWrapperFacade@aea0d43
out.print("ServletConfig对象是:" + config.toString());
out.print("
");
}
}
public class ConfigTestServlet2 extends GenericServlet {
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 获取ServletConfig对象
ServletConfig config = this.getServletConfig();
// 输出ServletConfig对象: org.apache.catalina.core.StandardWrapperFacade@287af686
out.print("ServletConfig对象是:" + config);
}
}
ServletConfig接口中常用的方法
Tomcat服务器会将init-param标签中的初始化参数信息封装到一个ServletConfig对象中,要想获取参数信息需要通过ServletConfig接口中的方法
方法名 | 方法作用 |
---|---|
public String getInitParameter(String name) | 通过init-param标签的name获取value |
public Enumeration< String > getInitParameterNames() | 获取所有init-param标签的name |
public ServletContext getServletContext() | 获取ServletContext对象(封装了上下文初始化参数信息的对象) |
public String getServletName() | 获取Servlet对象的name |
获取Servlet对象初始化参数方式
<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">
<servlet>
<servlet-name>configTestservlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.ConfigTestServletservlet-class>
<init-param>
<param-name>driverparam-name>
<param-value>com.mysql.cj.jdbc.Driverparam-value>
init-param>
<init-param>
<param-name>urlparam-name>
<param-value>jdbc:mysql://localhost:3306/bjpowernodeparam-value>
init-param>
<init-param>
<param-name>userparam-name>
<param-value>rootparam-value>
init-param>
<init-param>
<param-name>passwordparam-name>
<param-value>root1234param-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>configTestservlet-name>
<url-pattern>/testurl-pattern>
servlet-mapping>
web-app>
第一种: 通过父类的getServletConfig方法先获取ServletConfig对象,然后调用Config对象的方法获取Servlet对象的初始化参数
public class ConfigTestServlet extends GenericServlet {
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 通过父类的方法获取ServletConfig对象
ServletConfig config = this.getServletConfig();
// 获取 标签中的name
String servletName = config.getServletName();
out.print("" +servletName+"");//configTest
out.print("
");
// 获取所有初始化参数的name集合
Enumeration<String> initParameterNames = config.getInitParameterNames();
// 遍历集合
while(initParameterNames.hasMoreElements()) { // 是否有更多name元素
String parameterName = initParameterNames.nextElement(); // 取name元素
// 通过初始化参数的name获取value
String parameterVal = config.getInitParameter(parameterName);
out.print(parameterName + "=" + parameterVal);
out.print("
");
}
// 通过初始化参数的name获取value
String driver = config.getInitParameter("driver");
out.print(driver);
}
}
第二种: 直接使用this调用父类GenericServlet提供的获取初始化参数的方法,底层是让父类帮我们调用ServletConfig对象的获取初始化参数的方法
// GenericServlet源码
public abstract class GenericServlet implements Servlet {
// 成员变量
private ServletConfig config;
@Override
public final void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
@Override
public ServletConfig getServletConfig() {
return config;
}
// GenericServlet提供的获取初始化参数的方法
@Override
public ServletConfig getInitParameter(String name) {
return getServletConfig().getInitParameter(String name);
}
// GenericServlet提供的获取初始化参数的方法
@Override
public ServletConfig getInitParameterNames() {
return getServletConfig().getInitParameterNames();
}
}
// 测试程序
public class ConfigTestServlet extends GenericServlet {
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// 这里调用的是的GenericServlet提供的getInitParameterNames方法,底层还是调用ServletConfig对象的获取初始化参数的方法
Enumeration<String> names = this.getInitParameterNames();
while(names.hasMoreElements()){
String name = names.nextElement();
String value = this.getInitParameter(name);
System.out.println(name + "=" + value);
}
}
}