web.xml中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.這個監聽是自己寫的一個類,除了初始化方法,它還有銷燬方法.用於關閉應用前釋放資源.比如說資料庫連線的關閉.
如:
< !-- 載入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) {
//用於在容器關閉時,操作
}
//用於在容器開啟時,操作
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區別
web.xml裡面可以定義兩種引數:
(1)application範圍內的引數,存放在servletcontext中,在web.xml中配置如下:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
< /context-param>
(2)servlet範圍內的引數,只能在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("下面的兩個引數param1是在servlet中存放的");
System.out.println(this.getInitParameter("param1"));
System.out.println("下面的引數是存放在servletcontext中的");
System.out.println(getServletContext().getInitParameter("context/param"));
}
}
第一種引數在servlet裡面可以通過getServletContext().getInitParameter("context/param")得到
第二種引數只能在servlet的init()方法中通過this.getInitParameter("param1")取得.