Servlet之ServletConfig和ServletContext的探索

来源:互联网 发布:java 简单代码实例 编辑:程序博客网 时间:2024/05/01 19:23
HttpServlet 类 这是一个抽象类,继承了GenericServlet类,这个GenericSerlvet类实现了ServletConfig这个接口,所以它实现了方法
public ServletConfig getServletConfig()和 public ServletContext getServletContext();
这两个方法是如何实现的呢?在GenericServlet类中有一个ServletContext对象,在web容器初始化的时候就给这个ServletConfig属性赋予了StandardWrapperFacade 的对象,这个对象中有两个属性config和context分别为他们StandardWrapperApplicationContextFacade 类的对象,通过getServletConfig和getServletContext得到的就是这两个对象了。
我们可以通过继承HttpServlet的自定义Servlet中这两个函数得到ServletConfig和ServletContext对象。而不用得到ServletConfig然后调用getServletContext()方法。
下面看看ServletConfig 和 ServletContext的区别ServletConfig:从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet有效,一个servlet的ServletConfig对象不能被另一个servlet访问。ServletContext:对任何servlet,任何人在任何时间都有效,这才是真正全局的对象。
ServletContext提供getInitParameter()可以获取下边的指定参数
<context-param>
   <param-name>context_param_key1</param-name>
   <param-value>context_param_value1</param-value>
  </context-param>

ServletConfig提供的getInitParameter()可以获取下边指定的参数
<servlet>
 <servlet-name>Test</servlet-name>
 <servlet-class>xxx</servlet-class>
 <init-param>
 <param-name>absPath</param-name> 
 <param-value>/usr/mail/ax/axman/Maildir/</param-value> 
 </init-param>
 </servlet>
他们如何获取的呢?利用的是StandardWrapperApplicationContextFacade 中提供的方法
在ServletContext这个接口中提供了很多比较眼熟的方法:public String getRealPath(String path);public InputStream getResourceAsStream(String path);等等,这里想了解是在ServletContext中储存值 
提供了
public void setAttribute(String name, Object object);
public void removeAttribute(String name);
public Object getAttribute(String name);
是不是很熟悉,我想在ApplicationContextFacade中无外乎用集合存储,最终转化的数组。

接下来通过两个两段程序来验证上边的描述
package com.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 MyServletFirst extends HttpServlet{/** *  */private static final long serialVersionUID = 1L;@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//we store these values of variables in First servlet//and read the values of variables in the other//let's have a look that which variable is nullServletConfig servletConfig = this.getServletConfig();ServletContext servletContext = servletConfig.getServletContext();servletContext.setAttribute("key1", "value2");servletContext.setAttribute("key2", "value2");Enumeration enums = servletContext.getInitParameterNames();//get those global initialized variable which define in the web.xmlwhile(enums.hasMoreElements()){String key = (String)enums.nextElement();String value = servletContext.getInitParameter(key);servletContext.setAttribute(key, value);}//get those initialized variable which define in the servletString value = servletConfig.getInitParameter("init_param_key");System.out.println(value);}@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doPost(req,resp);}}
这里我写了两个servlet,MyServltFirst是第一个被访问的servlet类,主要的功能是在ServletContext中缓存好用户在web.xml中定义的context-param数据,和用setAttribute方法存放的数据。为了验证它是全局的,能在所有的servlet中被获取,另写了一个Servlet类:MyServletSecond
package com.servlet;import java.io.IOException;import java.util.Enumeration;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 MyServletSecond extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//in this servlet,we get values from ServletContext which insert in the MyServletFirst servletServletContext servletContext = this.getServletContext();Enumeration enums = servletContext.getAttributeNames();while(enums.hasMoreElements()){String key = (String) enums.nextElement();//due to the ServletContext object has store other Object of parameter//so here the variable of Type can't be String Object value = servletContext.getAttribute(key);System.out.println(key+"->"+value.toString());}}}
因为在ServletContext中除了从配置文件用户定义的一些上下文参数和在MyServletFirst存入的参数外,还有容器存放的一些对象,所以在上边接收getAttribute()方法返回值的对象引用,是Object类型,通过不同对象重写的toString()方法可以转换成各自形式的字符串






 



原创粉丝点击