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

servletConfig的应用

时间:2016-12-03 00:47:04      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:index   cal   int   stat   查看   load   数据库   root   more   

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


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


阅读ServletConfig API,并举例说明该对象的作用:
获得字符集编码
获得数据库连接信息
获得配置文件 ,查看struts案例的web.xml文件

 

 

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0"  
  3.     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  
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7.   <display-name></display-name
  8.   <servlet
  9.     <servlet-name>DemoServlet</servlet-name
  10.     <servlet-class>cn.csdn.web.servlet.DemoServlet</servlet-class
  11.      <init-param
  12.         <param-name>version</param-name
  13.         <param-value>2.0</param-value
  14.     </init-param>  
  15.     <init-param
  16.         <param-name>characterEncoding</param-name
  17.         <param-value>UTF-8</param-value
  18.     </init-param>  
  19.     <init-param
  20.         <param-name>driver</param-name
  21.         <param-value>com.mysql.jdbc.driver</param-value
  22.     </init-param>  
  23.     <init-param
  24.         <param-name>url</param-name
  25.         <param-value>jdbc:mysql://localhost:3306/3g</param-value
  26.     </init-param>  
  27.     <init-param
  28.         <param-name>user</param-name
  29.         <param-value>root</param-value
  30.     </init-param>  
  31.     <init-param
  32.         <param-name>password</param-name
  33.         <param-value>0904</param-value
  34.     </init-param>  
  35.     <load-on-startup>1</load-on-startup
  36.   </servlet
  37.  
  38.   <servlet-mapping
  39.     <servlet-name>DemoServlet</servlet-name
  40.     <url-pattern>/demo/*</url-pattern
  41.   </servlet-mapping
  42.   <servlet-mapping
  43.     <servlet-name>DemoServlet</servlet-name
  44.     <url-pattern>*.do</url-pattern
  45.   </servlet-mapping
  46.      
  47.   <welcome-file-list
  48.     <welcome-file>index.html</welcome-file
  49.   </welcome-file-list
  50. </web-app
 
  1. package cn.csdn.web.servlet; 
  2.  
  3. import java.io.IOException; 
  4. import java.io.PrintWriter; 
  5. import java.util.Enumeration; 
  6. import javax.servlet.ServletConfig; 
  7. import javax.servlet.ServletException; 
  8. import javax.servlet.http.HttpServlet; 
  9. import javax.servlet.http.HttpServletRequest; 
  10. import javax.servlet.http.HttpServletResponse; 
  11.  
  12. public class DemoServlet extends HttpServlet { 
  13.  
  14.     /**
  15.      * @author 宋鹤贤
  16.      */ 
  17.  
  18.     private static final long serialVersionUID = 1L; 
  19.  
  20.     /* private ServletConfig config; */ 
  21.  
  22.     @Override 
  23.     public void destroy() { 
  24.         // TODO Auto-generated method stub 
  25.         super.destroy(); 
  26.         System.out.println("销毁"); 
  27.     } 
  28.  
  29.     public DemoServlet() { 
  30.         super(); 
  31.         // TODO Auto-generated constructor stub 
  32.         System.out.println("创建对象"); 
  33.     } 
  34.  
  35.     @Override 
  36.     public void init() throws ServletException { 
  37.         // TODO Auto-generated method stub 
  38.         super.init(); 
  39.         System.out.println("初始化"); 
  40.     } 
  41.  
  42.     @Override 
  43.     public void init(ServletConfig config) throws ServletException { 
  44.         super.init(config); 
  45.         System.out.println("--------------" + config); 
  46.         /* this.config=config; */ 
  47.     } 
  48.  
  49.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  50.             throws ServletException, IOException { 
  51.         this.doPost(request, response); 
  52.     } 
  53.  
  54.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
  55.             throws ServletException, IOException { 
  56.         /* 设置编码 */ 
  57.         request.setCharacterEncoding("UTF-8"); 
  58.         String version = this.getServletConfig().getInitParameter("version"); 
  59.         String characterEncoding = this.getServletConfig().getInitParameter( 
  60.                 "characterEncoding"); 
  61.  
  62.         Enumeration<String> enumer = this.getServletConfig() 
  63.                 .getInitParameterNames(); 
  64.         // 遍历 
  65.  
  66.         while (enumer.hasMoreElements()) { 
  67.             String name = enumer.nextElement(); 
  68.             String value = this.getServletConfig().getInitParameter(name); 
  69.             System.out.println(name + "===" + value); 
  70.         } 
  71.  
  72.         /* 获取一下PrintWriter对象 */ 
  73.         PrintWriter out = response.getWriter(); 
  74.         out.print("versin=" + version + "\tcharacterEncoding=" 
  75.                 + characterEncoding); 
  76.     } 
  77.  
 


代码如上:我们发布到tomcat中,然后开启tomcat服务器,在浏览器中输入http://localhost:8080/20111018/xxx.do,界面返回结果如下:

技术分享

servletConfig的应用

标签:index   cal   int   stat   查看   load   数据库   root   more   

原文地址:http://www.cnblogs.com/keyi/p/6127570.html

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