posts - 93,  comments - 2,  trackbacks - 0
          <context-param>的作用:
          web.xml的配置中<context-param>配置作用
          1. 啟動(dòng)一個(gè)WEB項(xiàng)目的時(shí)候,容器(如:Tomcat)會(huì)去讀它的配置文件web.xml.讀兩個(gè)節(jié)點(diǎn): <listener></listener> 和 <context-param></context-param>
          2.緊接著,容器創(chuàng)建一個(gè)ServletContext(上下文),這個(gè)WEB項(xiàng)目所有部分都將共享這個(gè)上下文.
          3.容器將<context-param></context-param>轉(zhuǎn)化為鍵值對(duì),并交給ServletContext.
          4.容器創(chuàng)建<listener></listener>中的類實(shí)例,即創(chuàng)建監(jiān)聽.
          5.在監(jiān)聽中會(huì)有contextInitialized(ServletContextEvent args)初始化方法,在這個(gè)方法中獲得ServletContext = ServletContextEvent.getServletContext();
          context-param的值 = ServletContext.getInitParameter("context-param的鍵");
          6.得到這個(gè)context-param的值之后,你就可以做一些操作了.注意,這個(gè)時(shí)候你的WEB項(xiàng)目還沒有完全啟動(dòng)完成.這個(gè)動(dòng)作會(huì)比所有的Servlet都要早.
          換句話說,這個(gè)時(shí)候,你對(duì)<context-param>中的鍵值做的操作,將在你的WEB項(xiàng)目完全啟動(dòng)之前被執(zhí)行.
          7.舉例.你可能想在項(xiàng)目啟動(dòng)之前就打開數(shù)據(jù)庫.
          那么這里就可以在<context-param>中設(shè)置數(shù)據(jù)庫的連接方式,在監(jiān)聽類中初始化數(shù)據(jù)庫的連接.
          8.這個(gè)監(jiān)聽是自己寫的一個(gè)類,除了初始化方法,它還有銷毀方法.用于關(guān)閉應(yīng)用前釋放資源.比如說數(shù)據(jù)庫連接的關(guān)閉.
          如:
          <!-- 加載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>
          又如: --->自定義context-param,且自定義listener來獲取這些信息
          <context-param>
              <param-name>urlrewrite</param-name>
              <param-value>false</param-value>
          </context-param>
          <context-param>
              <param-name>cluster</param-name>
              <param-value>false</param-value>
          </context-param>
          <context-param>
              <param-name>servletmapping</param-name>
              <param-value>*.bbscs</param-value>
          </context-param>
          <context-param>
              <param-name>poststoragemode</param-name>
              <param-value>1</param-value>
          </context-param>
          <listener>
              <listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>
          </listener>
          public class SysListener extends HttpServlet implements ServletContextListener {
          private static final Log logger = LogFactory.getLog(SysListener.class);
          public void contextDestroyed(ServletContextEvent sce) {
            //用于在容器關(guān)閉時(shí),操作
          }
          //用于在容器開啟時(shí),操作
          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_3/
             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區(qū)別
          web.xml里面可以定義兩種參數(shù):
          (1)application范圍內(nèi)的參數(shù),存放在servletcontext中,在web.xml中配置如下:
          <context-param>
                     <param-name>context/param</param-name>
                     <param-value>avalible during application</param-value>
          </context-param>
          (2)servlet范圍內(nèi)的參數(shù),只能在servlet的init()方法中取得,在web.xml中配置如下:
          <servlet>
              <servlet-name>MainServlet</servlet-name>
              <servlet-class>com.wes.controller.MainServlet</servlet-class>
              <init-param>
                 <param-name>param1</param-name>
                 <param-value>avalible in servlet init()</param-value>
              </init-param>
              <load-on-startup>0</load-on-startup>
          </servlet>
          在servlet中可以通過代碼分別取用:
          package com.wes.controller;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          public class MainServlet extends HttpServlet ...{
              public MainServlet() ...{
                  super();
               }
              public void init() throws ServletException ...{
                   System.out.println("下面的兩個(gè)參數(shù)param1是在servlet中存放的");
                   System.out.println(this.getInitParameter("param1"));
                   System.out.println("下面的參數(shù)是存放在servletcontext中的");
                  System.out.println(getServletContext().getInitParameter("context/param"));
                }
          }
          第一種參數(shù)在servlet里面可以通過getServletContext().getInitParameter("context/param")得到
          第二種參數(shù)只能在servlet的init()方法中通過this.getInitParameter("param1")取得.


          文章來源自:http://www.cnblogs.com/hzj-/articles/1689836.html
          posted on 2013-05-27 11:45 Terry Zou 閱讀(179) 評(píng)論(0)  編輯  收藏 所屬分類: Tomcat+Eclipse
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 磐安县| 张掖市| 手游| 临清市| 梅州市| 宜兴市| 拜城县| 五台县| 师宗县| 唐海县| 平遥县| 朝阳县| 瑞金市| 志丹县| 克山县| 崇文区| 科尔| 类乌齐县| 龙岩市| 陈巴尔虎旗| 双峰县| 宁强县| 百色市| 肇州县| 拜城县| 邵东县| 永济市| 文山县| 宝清县| 和田市| 黄浦区| 绵阳市| 福鼎市| 金阳县| 丰城市| 普安县| 修水县| 平远县| 九寨沟县| 曲阳县| 右玉县|