Java登陆第二十九天——ServletConfig和ServletContext
ServletConfig
ServletConfig是web.xml中,Servlet的初始化参数对象(每个Servlet都各自拥有至少一个ServletConfig对象)
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">
<!--
每一个Servlet都可以配置多个初始化参数
-->
<servlet>
<servlet-name>Test3</servlet-name>
<servlet-class>com.example.JavaWeb.TestServlet3</servlet-class>
<!-- 初始化参数定义在Servlet标签之内 -->
<init-param>
<param-name>key</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test3</servlet-name>
<url-pattern>/Test3</url-pattern>
</servlet-mapping>
</web-app>
查看ServletConfig源码
Servlet在初始化阶段,Tomcat会自动生成ServletConfig对象并传入init(ServletConfig config)方法
ServletConfig是一个接口
public interface ServletConfig {
String getServletName();
ServletContext getServletContext();
String getInitParameter(String var1);
Enumeration<String> getInitParameterNames();
}
方法名 | 描述 |
---|---|
String getServletName() | 获取Servlet名称(web.xml中servlet-name) |
ServletContext getServletContext() | 获取ServletContext对象(马上就介绍) |
String getInitParameter(String var1) | 根据初始化参数名获取值(web.xml中param-name) |
Enumeration |
获取初始化参数迭代器(早期的迭代器就是Enumeration) |
读取初始化参数栗子
web.xml就是上面那个
TestServlet3
public class TestServlet3 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();//GenericServlet类提供的方法
Enumeration<String> es = servletConfig.getInitParameterNames();//早期的迭代器
while (es.hasMoreElements()) {
String s = es.nextElement();
System.out.println("param-name:"+s);
System.out.println("param-value:"+getInitParameter(s));
}
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test3
//控制台输出
param-name:key
param-value:value
在注解中设置初始化参数
TestServlet3
@WebServlet(
urlPatterns = "/Test3",
initParams = {@WebInitParam(name = "key",value = "value")}
)
public class TestServlet3 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();//GenericServlet类提供的方法
Enumeration<String> es = servletConfig.getInitParameterNames();//早期的迭代器
while (es.hasMoreElements()) {
String s = es.nextElement();
System.out.println("param-name:"+s);
System.out.println("param-value:"+getInitParameter(s));
}
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test3
//控制台输出
param-name:key
param-value:value
ServletContext
-
ServletContext是web.xml中,项目的初始化参数对象(每个项目仅有一个初始化参数对象)
-
ServletConfig是web.xml中,Servlet的初始化参数对象(每个Servlet都各自拥有至少一个ServletConfig对象)
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>key</param-name>
<param-value>value</param-value>
</context-param>
</web-app>
可以通过ServletConfig的getServletContext()方法获取ServletContext
@WebServlet(
urlPatterns = "/Test3",
initParams = {@WebInitParam(name = "key",value = "value")}
)
public class TestServlet3 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();//GenericServlet类提供的方法
Enumeration<String> es = servletConfig.getInitParameterNames();//早期的迭代器
while (es.hasMoreElements()) {
String s = es.nextElement();
System.out.println("param-name:"+s);
System.out.println("param-value:"+getInitParameter(s));
}
System.out.println("==============================");
ServletContext servletContext = getServletConfig().getServletContext();
Enumeration<String> ess = servletContext.getInitParameterNames();
while (ess.hasMoreElements()) {
String s = ess.nextElement();
System.out.println("context-name:"+s);
System.out.println("context-value:"+getInitParameter(s));
}
}
}
客户端请求URL:http://localhost:8080/JavaWeb_war_exploded/Test3
//控制台输出
param-name:key
param-value:value
==============================
context-name:key
context-value:value