码迷,mamicode.com
首页 > 其他好文 > 详细

关于ServletConfig的小结

时间:2014-10-22 06:16:31      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   使用   java   

     在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
示例代码如下:
 1  1 package com.yyz.servletconfig;
 2  2 
 3  3 import java.io.IOException;
 4  4 import java.util.Enumeration;
 5  5 
 6  6 import javax.servlet.ServletConfig;
 7  7 import javax.servlet.ServletException;
 8  8 import javax.servlet.http.HttpServlet;
 9  9 import javax.servlet.http.HttpServletRequest;
10 10 import javax.servlet.http.HttpServletResponse;
11 11 
12 12 public class ServletConfigDemo1 extends HttpServlet {
13 13 
14 14 
15 15 
16 16     ServletConfig config;
17 17     public void doGet(HttpServletRequest request, HttpServletResponse response)
18 18             throws ServletException, IOException {
19 19       //获取指定的初始化参数
20 20         String value = config.getInitParameter("xxx");
21 21         response.getOutputStream().write(value.getBytes());
22 22         //获取所有的初始化参数
23 23         Enumeration e = cofig.getInitParameterNames();
24 24         while(e.hasMoreElements()){
25 25             String name = (String) e.nextElement();
26 26             value = config.getInitParameter(name);
27 27             response.getOutputStream().write((name+"="+value+"<br/>").getBytes());
28 28         }
29 29     }
30 30 
31 31     
32 32     public void doPost(HttpServletRequest request, HttpServletResponse response)
33 33             throws ServletException, IOException {
34 34         doGet(request,response);
35 35 
36 36     
37 37     }
38 38 
39 39 
40 40     @Override
41 41     public void init(ServletConfig config) throws ServletException {
42 42         this.config = config;
43 43     }
44 44 
45 45 }

相应的web.xml如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 6     version="2.5">
 7   <servlet>
 8    
 9     <servlet-name>ServletConfigDemo1</servlet-name>
10     <servlet-class>com.yyz.servletconfig.ServletConfigDemo1</servlet-class>
11     <init-param>
12                <param-name>xxx</param-name>
13                <param-value>yyy</param-value>
14     </init-param>
15       <init-param>
16                <param-name>name</param-name>
17                <param-value>yyz</param-value>
18     </init-param>
19       <init-param>
20                <param-name>password</param-name>
21                <param-value>yyy</param-value>
22     </init-param>
23   </servlet>
24  <servlet-mapping>
25     <servlet-name>ServletConfigDemo1</servlet-name>
26     <url-pattern>/servlet/ServletConfigDemo1</url-pattern>
27   
28   </servlet-mapping>
29 </web-app>

测试结果如下:

 bubuko.com,布布扣

 在上面的代码中,ServletConfigDemo1对象中有一个ServletConfig对象,其实这是不必要的。因为ServletConfigDemo1继承了HttpServlet,HttpServlet又继承了GenericServlet GenericServlet 已经在内部维护了一个ServletConfig对象。相关实现如下:

1 public abstract class GenericServlet 
2     implements Servlet, ServletConfig, java.io.Serializable
3 {
4     …    …
5 private transient ServletConfig config;
6 public ServletConfig getServletConfig() {
7           return config;
8     }
9 }

因而我们可以通过我们写的Servlet对象的getServletConfig()方法直接拿到ServletConfig对象,示例代码如下:

 1  1 package com.yyz.servletconfig;
 2  2 
 3  3 import java.io.IOException;
 4  4 import java.io.PrintWriter;
 5  5 
 6  6 import javax.servlet.ServletException;
 7  7 import javax.servlet.http.HttpServlet;
 8  8 import javax.servlet.http.HttpServletRequest;
 9  9 import javax.servlet.http.HttpServletResponse;
10 10 
11 11 public class ServletConfigDemo2 extends HttpServlet {
12 12 
13 13     public void doGet(HttpServletRequest request, HttpServletResponse response)
14 14             throws ServletException, IOException {
15 15 
16 16         String value = this.getServletConfig().getInitParameter("name");
17 17         System.out.println(value);
18 18 }
19 19 
20 20     public void doPost(HttpServletRequest request, HttpServletResponse response)
21 21             throws ServletException, IOException {
22 22 
23 23         doGet(request, response);
24 24     }
25 25 
26 26 }

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
 <servlet>
    <servlet-name>ServletConfigDemo2</servlet-name>
    <servlet-class>com.yyz.servletconfig.ServletConfigDemo2</servlet-class>
     <init-param>
               <param-name>name</param-name>
               <param-value>yyz</param-value>
    </init-param>
  </servlet>
 <servlet-mapping>
    <servlet-name>ServletConfigDemo2</servlet-name>
    <url-pattern>/servlet/ServletConfigDemo2</url-pattern>
  </servlet-mapping>
</web-app>


 

 

 

 

 

 

 

 

  

 

 

关于ServletConfig的小结

标签:style   blog   http   color   io   os   ar   使用   java   

原文地址:http://www.cnblogs.com/yyz666/p/4042101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!