1. 程式人生 > >Web.xml的與的使用與區別

Web.xml的與的使用與區別

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.這個監聽是自己寫的一個類,除了初始化方法,它還有銷燬方法.用於關閉應用前釋放資源.比如說資料庫連線的關閉.
舉例說明:
<!-- 載入spring的配置檔案 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/jason-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
自定義Listener
public class SysListener extends HttpServlet implements ServletContextListener {
   private static final Log logger = LogFactory.getLog(SysListener.class);
   //用於在容器關閉時,操作
   public void contextDestroyed(ServletContextEvent sce) {   
   }
   //用於在容器開啟時,操作
   public void contextInitialized(ServletContextEvent sce) {
       String rootpath = sce.getServletContext().getRealPath("/");
       System.out.println("-------------rootPath:"+rootpath);   
       if (rootpath != null) {
          rootpath = rootpath.replaceAll("\\\\", "/");
       } else {
          rootpath = "/";
       }
       if (!rootpath.endsWith("/")) {
            rootpath = rootpath + "/";
       }
       Constant.ROOTPATH = rootpath;
       logger.info("Application Run Path:" + rootpath);
       String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");
       boolean burlrewrtie = false;
       if (urlrewrtie != null) {
           burlrewrtie = Boolean.parseBoolean(urlrewrtie);
       }
       Constant.USE_URL_REWRITE = burlrewrtie;
       logger.info("Use Urlrewrite:" + burlrewrtie);
       其它略之....    
   }

}

/*最終輸出
   -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Application Run Path:D:/tomcat_bbs/webapps/BBSCS_8_0
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Urlrewrite:true
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Use Cluster:false
   2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]SERVLET MAPPING:*.bbscs
   2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]Post Storage Mode:1
   */

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;publicclass MainServlet extends HttpServlet {   
  3. public MainServlet() {  
  4.         super();  
  5.     }  
  6.     publicvoid init() throws ServletException ...{  
  7.          System.out.println("下面的兩個引數param1是在servlet中存放的");  
  8.          System.out.println(this.getInitParameter("param1"));  
  9.          System.out.println("下面的引數是存放在servletcontext中的");  
  10.          System.out.println(getServletContext().getInitParameter("context/param"));  
  11.       }  
  12. }  

第一種引數在servlet裡面可以通過getServletContext().getInitParameter("context/param")得到
第二種引數只能在servlet的init()方法中通過this.getInitParameter("param1")取得.