首页 > 代码库 > ServletConfig对象

ServletConfig对象

在Servlet的配置文件中,可以使用一个或者多个<init-param>标签为servlet配置一些初始化参数。

当servlet配置了初始化常熟后,web容器在常见servlet实例对象时,会自动将这些初始化参数封装到servletconfig对象中,并在调用servlet的init方法时,将servletconfig对象传递给servlet,进而,程序员通过servletconfig对象就可以得到当前servlet的初始化参数信息。

 

阅读ServletConfig API,并举例说明该对象的作用:

获得字符集编码

获得数据库连接信息

获得配置文件,查看structs案例的web.xml文件

 1 package cn.itcast.servlet;
 2 
 3 import java.io.IOException;
 4 import java.util.Enumeration;
 5 
 6 import javax.servlet.ServletConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 
14 public class ConfigServletDemo1 extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16     private ServletConfig config;
17     
18     
19 
20     @Override
21     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22         //获取指定的初始化参数
23         String value = http://www.mamicode.com/config.getInitParameter("xxx");
24         resp.getOutputStream().write(value.getBytes());
25         
26         //获取所有初始化参数
27         Enumeration e = config.getInitParameterNames();
28         while(e.hasMoreElements()){
29             String name = (String) e.nextElement();
30             value =http://www.mamicode.com/ config.getInitParameter(name);
31             resp.getOutputStream().write((name + "=" + value + "<br />").getBytes());
32         }
33     }
34     
35     /**
36      * 这个方法有什么用?
37      * 1.配置初始化参数,配置字符集
38      * <init-param>
39           <param-name>charset</param-name>
40           <param-value>utf-8</param-value>
41       </init-param>
42      * 2.配置数据库连接设置
43      * <init-param>
44      *     <param-name>url</param-name>
45      *     <param-value>jdbc:mysql://localhost:3306/test</param-value>
46      * </init-param>
47      * <init-param>
48      *     <param-name>username</param-name>
49      *     <param-value>root</param-value>
50      * </init-param>
51      * <init-param>
52      *     <param-name>password</param-name>
53      *     <param-value>root</param-value>
54      * </init-param>
55      * 3.
56      */
57     
58 
59 
60 
61     @Override
62     public void init(ServletConfig config) throws ServletException {
63         this.config = config;
64     }
65        
66     
67 
68 }

下面的方法更简洁,在以后的开发过程中使用下面的方法

 1 package cn.itcast.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletConfig;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet implementation class ConfigServletDemo2
14  */
15 
16 public class ConfigServletDemo2 extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18        
19     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
20         ServletConfig config = this.getServletConfig();
21         String value = http://www.mamicode.com/config.getInitParameter("xxx");
22         response.getOutputStream().write(value.getBytes());
23     }
24 
25     
26     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
27         // TODO Auto-generated method stub
28         doGet(request, response);
29     }
30 
31 }

 

ServletConfig对象