[Servlet&JSP] 初识ServletConfig

每个Servlet都必须在web.xml中设置,由web容器读取Servlet设置、初始化等,才可以真正成为一个Servlet。在web.xml中对于每个Servlet的设置,web容器会为其生成一个ServletConfig作为代表对象,你可以从该对象取得设置在web.xml中的Servlet初始参数,以及代表整个web应用程序的ServletContext对象。

Servlet生命周期

在Servlet接口上,定义了与Servlet生命周期及请求服务相关的init()service()destroy()是三个方法。在web容器启动后,会读取web.xml的设置,根据其中每个Servlet的设置,将Servlet类加载并实例化,并为每个Servlet的设置生成一个ServletConfig对象,而后调用Servlet接口的init()方法,将产生的ServletConfig对象当做参数传入。

GenericServlet同时实现了Servlet及ServletConfig,GenericServlet的主要目的就是将初始Servlet调用init()方法所传入的ServletConfig封装起来。GenericServlet在实现Servlet的init()方法时,也调用了灵感而无参数的init()方法,基本上在编写Servlet时,如果有一些初始时所要执行的操作,则可以重写这个无参数的init()方法,而不是直接重写有ServletConfig参数的init()方法。

GenericServlet也包括了Servlet与ServletConfig所定义方法的简单实现,实现内容主要是通过ServletConfig来取得一些相关信息,例如:

  /**
   * Returns a <code>String</code> containing the value of the named
   * initialization parameter, or <code>null</code> if the parameter does not
   * exist. See {@link ServletConfig#getInitParameter}.
   * <p>
   * This method is supplied for convenience. It gets the value of the named
   * parameter from the servlet's <code>ServletConfig</code> object.
   *
   * @param name
   *            a <code>String</code> specifying the name of the
   *            initialization parameter
   * @return String a <code>String</code> containing the value of the
   *         initialization parameter
   */
  @Override
  public String getInitParameter(String name) {
      return getServletConfig().getInitParameter(name);
  }

  /**
   * Returns the names of the servlet's initialization parameters as an
   * <code>Enumeration</code> of <code>String</code> objects, or an empty
   * <code>Enumeration</code> if the servlet has no initialization parameters.
   * See {@link ServletConfig#getInitParameterNames}.
   * <p>
   * This method is supplied for convenience. It gets the parameter names from
   * the servlet's <code>ServletConfig</code> object.
   *
   * @return Enumeration an enumeration of <code>String</code> objects
   *         containing the names of the servlet's initialization parameters
   */
  @Override
  public Enumeration<String> getInitParameterNames() {
      return getServletConfig().getInitParameterNames();
  }

  /**
   * Returns this servlet's {@link ServletConfig} object.
   *
   * @return ServletConfig the <code>ServletConfig</code> object that
   *         initialized this servlet
   */
  @Override
  public ServletConfig getServletConfig() {
      return config;
  }

  /**
   * Returns a reference to the {@link ServletContext} in which this servlet
   * is running. See {@link ServletConfig#getServletContext}.
   * <p>
   * This method is supplied for convenience. It gets the context from the
   * servlet's <code>ServletConfig</code> object.
   *
   * @return ServletContext the <code>ServletContext</code> object passed to
   *         this servlet by the <code>init</code> method
   */
  @Override
  public ServletContext getServletContext() {
      return getServletConfig().getServletContext();
  }

  /**
   * Returns information about the servlet, such as author, version, and
   * copyright. By default, this method returns an empty string. Override this
   * method to have it return a meaningful value. See
   * {@link Servlet#getServletInfo}.
   *
   * @return String information about this servlet, by default an empty string
   */
  @Override
  public String getServletInfo() {
      return "";
  }

所以当在继承HttpServlet实现Servlet时,就可以通过这些方法来取得所要的相关信息,而不用直接意识到ServletConfig的存在。

Servlet初始参数的设置和取得

ServletConfig相当于web.xml中个别Servlet的设置代表对象,这意味着可以从ServletConfig中取得Servlet设置信息。ServletConfig定义了getInitParameter()getInitParameterNames()方法,可以让你取得设置Servlet时的初始参数。
若要在web.xml中设置个别Servlet的初始参数,可以在<servlet>标签之中,使用<init-param>进行设置。例如:

<servlet>
    <servlet-name>SomeServlet</servlet-name>
    <servlet-class>club.chuxing.SomeServlet</servlet-class>
    <init-param>
        <param-name>PARAM1</param-name>
        <param-value>VALUE1</param-value>
    </init-param>
    <init-param>
        <param-name>PARAM2</param-name>
        <param-value>VALUE2</param-value>
    </init-param>
</servlet>

对于上述的例子,只要取得对应SomeServlet设置的ServletConfig实例,就可以通过geInitParameter()并指定<param-name>的名称来取得<param-value>的值。例如:

public class AddMessage extends HttpServlet {
    private String PARAM1;
    private String PARAM2;
    public void init() throws ServletException {
        super.init();
        PARAM1 = getServletConfig().getInitParameter("PARAM1");
        PARAM2 = getServletConfig().getInitParameter("PARAM2");
    }
}

由于ServletConfig必须在Web容器将Servlet实例化后,调用有参数的init()方法再将之传入,是与Web应用程序资源相关的对象,所以在继承HttpServlet后,通常会重写无参数的init()方法以取得Servlet的初始参数。由于GenericServlet定义了一些方法将ServletConfig封装起来,便于取得设置信息,以上代码可改写为:

public class AddMessage extends HttpServlet {
    private String PARAM1;
    private String PARAM2;
    public void init() throws ServletException {
        super.init();
        PARAM1 = getInitParameter("PARAM1");
        PARAM2 = getInitParameter("PARAM2");
    }
}

Servlet初始参数通常作为常数来设置,可以将一些Servlet程序中不想写死的信息放到初始参数中,之后若想更改这些信息,则只修改web.xml中的设置,而不用修改源代码、重新编译或部署。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值