<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/act 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; imp
imp 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")取得. |
|