标签:des zed slist extc 自己 framework 执行 com org
web.xml的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
1 <!-- 加载spring的配置文件 -->
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-
5 INF/jason-servlet.xml</param-value>
6 </context-param>
7 <listener>
8 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
9 </listener>
1 <context-param>
2 <param-name>urlrewrite</param-name>
3 <param-value>false</param-value>
4 </context-param>
5 <context-param>
6 <param-name>cluster</param-name>
7 <param-value>false</param-value>
8 </context-param>
9 <context-param>
10 <param-name>servletmapping</param-name>
11 <param-value>*.bbscs</param-value>
12 </context-param>
13 <context-param>
14 <param-name>poststoragemode</param-name>
15 <param-value>1</param-value>
16 </context-param>
17 <listener>
18 <listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>
19 </listener>
1 public class SysListener extends HttpServlet implements ServletContextListener
2 {
3 private static final Log logger = LogFactory.getLog(SysListener.class);
4
5 public void contextDestroyed(ServletContextEvent sce)
6 {
7 // 用于在容器关闭时,操作
8 }
9 // 用于在容器开启时,操作
10 public void contextInitialized(ServletContextEvent sce)
11 {
12 String rootpath = sce.getServletContext().getRealPath("/");
13 System.out.println("-------------rootPath:"+rootpath);
14 if (rootpath != null)
15 {
16 rootpath = rootpath.replaceAll("\\\\", "/");
17 } else
18 {
19 rootpath = "/";
20 }
21 if (!rootpath.endsWith("/"))
22 {
23 rootpath = rootpath + "/";
24 }
25 Constant.ROOTPATH = rootpath;
26 logger.info("Application Run Path:" + rootpath);
27 String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");
28 boolean burlrewrtie = false;
29 if (urlrewrtie != null)
30 {
31 burlrewrtie = Boolean.parseBoolean(urlrewrtie);
32 }
33 Constant.USE_URL_REWRITE = burlrewrtie;
34 logger.info("Use Urlrewrite:" + burlrewrtie);
35 其它略之....
36 }
37 }
38 /*
39 * 最终输出 -------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\ 2009-06-09
40 * 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Application Run
41 * Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/ 2009-06-09 21:51:46,526
42 * [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Use Urlrewrite:true
43 * 2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Use
44 * Cluster:false 2009-06-09 21:51:46,526
45 * [com.laoer.bbscs.web.servlet.SysListener]-[INFO] SERVLET MAPPING:*.bbscs
46 * 2009-06-09 21:51:46,573 [com.laoer.bbscs.web.servlet.SysListener]-[INFO] Post
47 * Storage Mode:1
48 */
1 <context-param>
2 <param-name>context/param</param-name>
3 <param-value>avalible during application</param-value>
4 </context-param>
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>
1 package com.wes.controller;
2 import javax.servlet.ServletException;
3 import javax.servlet.http.HttpServlet;
4 public class MainServlet extends HttpServlet ...
5 {
6 public MainServlet() ...
7 {
8 super();
9 }
10 public void init() throws ServletException ...
11 {
12 System.out.println("下面的两个参数param1是在servlet中存放的");
13 System.out.println(this.getInitParameter("param1"));
14 System.out.println("下面的参数是存放在servletcontext中的");
15 System.out.println(getServletContext().getInitParameter("context/param"));
16 }
17 }
标签:des zed slist extc 自己 framework 执行 com org
原文地址:http://www.cnblogs.com/guanghe/p/6129061.html