[转] <context-param>与<init-param>的区别与作用

看到一篇关于web.xm文件中标签的讲解,顺带还阐述了容器的工作流程,因此转载此,以供参考,原文地址:与的区别与作用

<context-param>的作用:
web.xml的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的键");
6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.

如:
  1. <!-- 加载spring的配置文件 -->  
  2. <context-param>  
  3.     <param-name>contextConfigLocation</param-name>  
  4.     <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/jason-servlet.xml</param-value>  
  5. </context-param>  
  6. <listener>  
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8. </listener>  

  1. public class SysListener extends HttpServlet implements ServletContextListener 
  2. {
  3.     private static final Log logger = LogFactory.getLog(SysListener.class);
  4.     public void contextDestroyed(ServletContextEvent sce) 
  5.     {   
  6.     //用于在容器关闭时,操作  
  7.     }
  8.     //用于在容器开启时,操作
  9.     public void contextInitialized(ServletContextEvent sce) 
  10.     {  
  11.        String rootpath = sce.getServletContext().getRealPath("/");  
  12.        System.out.println("-------------rootPath:"+rootpath);   
  13.        if (rootpath != null) 
  14.        {  
  15.           rootpath = rootpath.replaceAll("\\\\", "/");  
  16.        } 
  17.        else 
  18.        {  
  19.          rootpath = "/";  
  20.      }  
  21.    if (!rootpath.endsWith("/")) 
  22.    {  
  23.       rootpath = rootpath + "/";  
  24.     }  
  25.    Constant.ROOTPATH = rootpath;  
  26.    logger.info("Application Run Path:" + rootpath);  
  27.    String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");  
  28.    boolean burlrewrtie = false;  
  29.    if (urlrewrtie != null) 
  30.  {  
  31.     burlrewrtie = Boolean.parseBoolean(urlrewrtie);  
  32.    }  
  33.    Constant.USE_URL_REWRITE = burlrewrtie;  
  34.    logger.info("Use Urlrewrite:" + burlrewrtie);  
  35.    其它略之....    
  36.   }
  37. }  
  38.    /*最终输出 
  39.    -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\ 
  40.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Application Run Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/ 
  41.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Urlrewrite:true 
  42.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Cluster:false 
  43.    2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]SERVLET MAPPING:*.bbscs 
  44.    2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Post Storage Mode:1 
  45.    */  

context-param和init-param区别
web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
  1. <context-param>  
  2.            <param-name>context/param</param-name>  
  3.            <param-value>avalible during application</param-value>  
  4. </context-param>  

(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
  1. <servlet>  
  2.     <servlet-name>MainServlet</servlet-name>  
  3.     <servlet-class>com.wes.controller.MainServlet</servlet-class>  
  4.     <init-param>  
  5.        <param-name>param1</param-name>  
  6.        <param-value>avalible in servlet init()</param-value>  
  7.     </init-param>  
  8.     <load-on-startup>0</load-on-startup>  
  9. </servlet>  

在servlet中可以通过代码分别取用:
  1. package com.wes.controller;import javax.servlet.ServletException;  
  2. import javax.servlet.http.HttpServlet;public class MainServlet extends HttpServlet ...{    public MainServlet() ...{  
  3.         super();  
  4.      }  
  5.     public void init() throws ServletException ...{  
  6.          System.out.println("下面的两个参数param1是在servlet中存放的");  
  7.          System.out.println(this.getInitParameter("param1"));  
  8.          System.out.println("下面的参数是存放在servletcontext中的");  
  9.         System.out.println(getServletContext().getInitParameter("context/param"));  
  10.       }  
  11. }  


第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到
第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得.

posted @ 2015-08-30 23:09  yunlvrensheng  阅读(308)  评论(0编辑  收藏  举报