原文同步发表至个人博客【夜月归途】
原文链接:http://www.guitu18.com/se/java/2018-07-26/20.html
出处:http://www.guitu18.com/
本博客中未标明转载的文章归作者夜月归途和博客园所有。
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
ServletConfig
继续查看ServletConfig源码:
package javax.servlet;
public interface ServletConfig {
public String getServletName();
public ServletContext getServletContext();
public String getInitParameter(String name);
public Enumeration<String> getInitParameterNames();
}
getServletName();
getServletName方法用于获取当前Servlet名称,这个是在xml中配置的:<servlet-name>springmvcdemo</servlet-name>;
getServletContext();
getServletContext方法用于获取代表当前web应用的ServletContext对象;
getInitParameter(String name);
getInitParameter方法用于获取当前Servlet指定名称的初始化参数的值;
getInitParameterNames();
getInitParameterNames获取当前Servlet所有初始化参数的名字枚举集合;
Servlet容器初始化时每一个Servlet对象时,会为其创建一个ServletConfig对象,通过Servlet的init方法,会把ServletContext对象传递给当前Servlet:
public void init(ServletConfig config) throws ServletException;
之后使用this.getServletConfig()对象获取当前Servlet的ServletConfig对象;
ServletConfig servletConfig = getServletConfig();
我们在xml中配置Servlet时,还可以配置Servlet初始化参数信息:
<servlet>
<servlet-name>Servlet_1</servlet-name>
<servlet-class>com.guitu.servlet.Servlet_1</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>zhangkuan</param-value>
</init-param>
</servlet>
之后可以通过servletConfig获取InitParameter参数;
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
获得的是一个参数名字的枚举集合,在调用getInitParameter(String name)即可获得参数的值:
String initParameter = servletConfig.getInitParameter(parameterName);
ServletContext
Servlet容器启动后,会为每一个独立的Web应用创建一个ServletContext实例,代表当前Web应用;
ServletContext由所有Servlet共享,所以ServletContext中的参数所有Servlet都能够获取,多个web组件之间使用它实现数据共享;
ServletContext常用方法:
getAttribute(String name);
获取ServletContext中存储的数据;
setAttribute(String name, Object obj);
向ServletContext中存储数据;
getInitParameter(String name);
获取<context-param>标签中为整个Web应用配置的初始化参数;
getInitParameterNames();
一次性获取<context-param>里所有的初始化参数名的枚举;
getRealPath(String path);
获取应用程序内指定资源的绝对路径;
getResource(String parh);
path必须是/开头,代表当前web应用程序的根目录。返回一个代表某个资源的URL对象;
getResoutceAsStream(String parh);
可以使用相对于根目录的路径访问到web目录下的所有文件,而不必知道绝对路径,返回文件流;
ServletConfig、ServletContext测试Demo
1、Servlet_1:
package com.guitu.servlet; import java.io.IOException;
import java.util.Enumeration; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Servlet_1 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ServletConfig servletConfig = getServletConfig();
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
System.out.println("=====Servlet_1.ServletConfig.InitParameter=====");
while (initParameterNames.hasMoreElements()) {
String parameterName = initParameterNames.nextElement();
String initParameter = servletConfig.getInitParameter(parameterName);
System.out.println(parameterName + ":" + initParameter);
} System.out.println("=====Servlet_1.ServletContext.InitParameter=====");
ServletContext servletContext = getServletContext();
Enumeration<String> contextInitParameters = servletContext.getInitParameterNames();
while (contextInitParameters.hasMoreElements()) {
String parameterName = contextInitParameters.nextElement();
String initParameter = servletContext.getInitParameter(parameterName);
System.out.println(parameterName + ":" + initParameter);
} servletContext.setAttribute("message", "Hello World !"); } protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
2、Servlet_2:
package com.guitu.servlet; import java.io.IOException;
import java.util.Enumeration; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Servlet_2 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ServletConfig servletConfig = getServletConfig();
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
System.out.println("=====Servlet_2.ServletConfig.InitParameter======");
while (initParameterNames.hasMoreElements()) {
String parameterName = initParameterNames.nextElement();
String initParameter = servletConfig.getInitParameter(parameterName);
System.out.println(parameterName + ":" + initParameter);
} System.out.println("=====Servlet_2.ServletContext.InitParameter=====");
ServletContext servletContext = getServletContext();
Enumeration<String> contextInitParameters = servletContext.getInitParameterNames();
while (contextInitParameters.hasMoreElements()) {
String parameterName = contextInitParameters.nextElement();
String initParameter = servletContext.getInitParameter(parameterName);
System.out.println(parameterName + ":" + initParameter);
} System.out.println("=====servletContext.getAttribute=====");
Object object = servletContext.getAttribute("message");
System.out.println("message:" + object);
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
3、web.xml配置:
<context-param>
<param-name>domain</param-name>
<param-value>guitu18.com</param-value>
</context-param>
<context-param>
<param-name>name</param-name>
<param-value>夜月归途</param-value>
</context-param>
<servlet>
<servlet-name>Servlet_1</servlet-name>
<servlet-class>com.guitu.servlet.Servlet_1</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>zhangkuan</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet_1</servlet-name>
<url-pattern>/Servlet_1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet_2</servlet-name>
<servlet-class>com.guitu.servlet.Servlet_2</servlet-class>
<init-param>
<param-name>password</param-name>
<param-value>Guitu18</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet_2</servlet-name>
<url-pattern>/Servlet_2</url-pattern>
</servlet-mapping>
在Servlet1和Servlet2中,分别获得了ServletConfig对象和ServletContext对象;
在web.xml中,也分别为他们配置了不同的初始化参数,同时配置了一个context级的初始化参数;
之后分别在两个Servlet中获取ServletConfig和ServletContext的InitParamer参数;
在Servlet1中还通过setAttribute向ServletContext域中保存了一条消息message,在Servlet2中,便可以通过getAttribute获取这条消息;
4、访问两个Servlet,先访问Servlet_1:
=====Servlet_1.ServletConfig.InitParameter=====
username:zhangkuan
=====Servlet_1.ServletContext.InitParameter=====
domain:guitu18.com
name:夜月归途
再访问Servlet_2:
=====Servlet_2.ServletConfig.InitParameter=====
password:Guitu18
=====Servlet_2.ServletContext.InitParameter=====
domain:guitu18.com
name:夜月归途
=====servletContext.getAttribute=====
message:Hello World !
可以看到,每个Servlet拥有自己ServletConfig对象,数据不互通,而ServletContext中的初始化参数和context域中的参数,是所有Servlet共享的,可以使用context域实现数据共享;