Servlet ServletConfig配置信息

ServletConfig對象是由每個servlet的Web容器創建的。這個對象可用於從web.xml文件獲取配置信息。

如果從web.xml文件修改配置信息,就不需要更改servlet中的代碼。因此,對於有特定配置內容需要不定時修改,這樣會更容易管理Web應用程序。

ServletConfig的優點

ServletConfig的核心優點是,如果可以修改web.xml文件中的信息,則不需要編輯servlet代碼文件。

ServletConfig接口的方法

編號

方法

描述

1

public String getInitParameter(String name)

返回指定參數名稱的參數值。

2

public Enumeration getInitParameterNames()

返回所有初始化參數名稱的枚舉。

3

public String getServletName()

返回servlet的名稱。

4

public ServletContext getServletContext()

返回ServletContext的對象。

如何獲取ServletConfig的對象?

  • 可通過調用Servlet接口的getServletConfig()方法來返回ServletConfig對象。

getServletConfig()方法的語法

public ServletConfig getServletConfig();

getServletConfig()方法的示例

ServletConfig config=getServletConfig();  
//Now we can call the methods of ServletConfig interface

爲servlet提供初始化參數的語法

servlet的init-param子元素用於指定servlet的初始化參數。

<web-app>
  <servlet>
    ......
    <init-param>  
      <param-name>parameter_name</param-name>
      <param-value>parameter_value</param-value>
    </init-param>
    ......
  </servlet>
</web-app>

獲取初始化參數的ServletConfig示例

在這個例子中,將從web.xml文件獲取一個初始化參數,並在servlet中打印這些信息。

創建一個名稱爲:ServletConfig的動態Web應用項目,其結構如下所示 -

Servlet

下面是幾個主要的代碼文件 -

文件:ConfigServlet.java -

package com.yiibai;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ConfigServlet
 */
public class ConfigServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        ServletConfig config = getServletConfig();
        String driver = config.getInitParameter("mysql_driver");
        out.print("Driver details is: <b>" + driver+"</b>");
        out.close();
    }
}

文件:web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>ConfigServlet</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>ConfigServlet</servlet-name>
        <servlet-class>com.yiibai.ConfigServlet</servlet-class>
        <init-param>
            <param-name>mysql_driver</param-name>
            <param-value>com.mysql.jdbc.Driver</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>ConfigServlet</servlet-name>
        <url-pattern>/dbconfig</url-pattern>
    </servlet-mapping>
</web-app>

將上面項目部署並訪問URL: http://localhost:8080/ConfigServlet/dbconfig ,如果程序沒有問題,那麼應該會看到以下結果 -

Servlet

獲取所有初始化參數的ServletConfig示例

在這個例子中,我們從web.xml文件獲取所有的初始化參數,並在servlet中打印這些信息。

幾個主要代碼文件如下 -

文件:ConfigServletAll.java -

package com.yiibai;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ConfigServletAll
 */
public class ConfigServletAll extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        ServletConfig config = getServletConfig();
        Enumeration<String> e = config.getInitParameterNames();
        String str = "";
        while (e.hasMoreElements()) {
            str = e.nextElement();
            out.print("Name: " + str);
            out.print(" value: " + config.getInitParameter(str)+"<br/>");
        }

        out.close();
    }

}

文件:web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>ConfigServlet</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>ConfigServlet</servlet-name>
        <servlet-class>com.yiibai.ConfigServlet</servlet-class>
        <init-param>
            <param-name>mysql_driver</param-name>
            <param-value>com.mysql.jdbc.Driver</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>ConfigServlet</servlet-name>
        <url-pattern>/dbconfig</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ConfigServletAll</servlet-name>
        <servlet-class>com.yiibai.ConfigServletAll</servlet-class>
        <init-param>
            <param-name>mssql_driver</param-name>
            <param-value>com.mssql.jdbc.Driver</param-value>
        </init-param>
        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>passwd</param-name>
            <param-value>123456</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>ConfigServletAll</servlet-name>
        <url-pattern>/dbconfigall</url-pattern>
    </servlet-mapping>

</web-app>

將上面項目部署並訪問URL: http://localhost:8080/ConfigServlet/dbconfigall ,如果程序沒有問題,那麼應該會看到以下結果 -

Servlet