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应用项目,其结构如下所示 -
下面是几个主要的代码文件 -
文件: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 ,如果程序没有问题,那么应该会看到以下结果 -
获取所有初始化参数的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 ,如果程序没有问题,那么应该会看到以下结果 -