[Servlet&JSP] 初识ServletConfig

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

每个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中的设置,而不用修改源代码、重新编译或部署。

目录
相关文章
|
2月前
|
SQL Java 数据库
jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上
该博客文章介绍了在JSP应用中使用Servlet查询SQL Server数据库的表信息,并通过JavaBean封装图书信息,将查询结果展示在Web页面上的方法。
jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上
|
2月前
|
缓存 安全 Java
Java服务器端技术:Servlet与JSP的集成与扩展
Java服务器端技术:Servlet与JSP的集成与扩展
22 3
|
2月前
|
存储 缓存 前端开发
Servlet与JSP在Java Web应用中的性能调优策略
Servlet与JSP在Java Web应用中的性能调优策略
25 1
|
2月前
|
供应链 前端开发 Java
JSP+servlet+mybatis+layui服装库存管理系统(大三上学期课程设计)
这篇文章通过一个服装库存管理系统的实例,展示了在Spring Boot项目中使用Ajax、JSON、layui、MVC架构和iframe等技术,涵盖了注册登录、权限管理、用户管理、库存管理等功能,并提供了系统运行环境和技术要求的详细说明。
JSP+servlet+mybatis+layui服装库存管理系统(大三上学期课程设计)
|
2月前
|
存储 Java 关系型数据库
基于Servlet和JSP的Java Web应用开发指南
基于Servlet和JSP的Java Web应用开发指南
18 0
|
2月前
|
前端开发 安全 Java
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
在Java服务器端开发的浩瀚宇宙中,Servlet与JSP犹如两颗璀璨的明星,它们联袂登场,共同编织出动态网站的绚丽篇章。
17 0
|
3月前
|
XML Java 数据格式
jsp和servlet有什么区别?
总的来说,JSP和Servlet都是创建动态Web应用程序的重要工具,但它们的使用依赖于特定的需求和上下文。
28 0
|
3月前
|
XML Java 数据格式
jsp和servlet有什么区别?
总的来说,JSP和Servlet都是创建动态Web应用程序的重要工具,但它们的使用依赖于特定的需求和上下文。
32 0
|
4月前
|
自然语言处理 前端开发 Java
Servlet与JSP:Java Web开发的基石技术详解
【6月更文挑战第23天】Java Web的Servlet与JSP是动态网页的核心。Servlet是服务器端的Java应用,处理HTTP请求并响应;JSP则是结合HTML与Java代码的页面,用于动态内容生成。Servlet通过生命周期方法如`init()`、`service()`和`destroy()`工作,而JSP在执行时编译成Servlet。两者在MVC架构中分工,Servlet处理逻辑,JSP展示数据。尽管有Spring MVC等框架,Servlet和JSP仍是理解Web开发基础的关键。
87 12
|
4月前
|
存储 Java 关系型数据库
基于Servlet和JSP的Java Web应用开发指南
【6月更文挑战第23天】构建Java Web应用,Servlet与JSP携手打造在线图书管理系统,涵盖需求分析、设计、编码到测试。通过实例展示了Servlet如何处理用户登录(如`LoginServlet`),JSP负责页面展示(如`login.jsp`和`bookList.jsp`)。应用基于MySQL数据库,包含用户和图书表。登录失败显示错误信息,成功后展示图书列表。部署到Tomcat服务器测试功能。此基础教程为深入Java Web开发奠定了基础。
84 10