Web.xml的與的使用與區別
阿新 • • 發佈:2019-02-11
web.xml的配置中<context-param>配置作用:
1.啟動一個WEB專案的時候,容器(如:Tomcat)會去讀它的配置檔案web.xml.讀兩個節點: <listener></listener> 和 <context-param></context-param>
舉例說明:
自定義Listener<!-- 載入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>
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中配置如下:
- <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;publicclass MainServlet extends HttpServlet {
- public MainServlet() {
- super();
- }
- publicvoid 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")取得.