<context-param>的作用:
l的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件l.读两个节点: <listener></listener> 和 <context-param></context-param>真实的风景
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContext();
context-param的值 = InitParameter("context-param的键");
6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.尊敬老师的名言
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/l,/l,/WEB-
l</param-value>
</context-param>
<listener>
<listener-class>org.t.ContextLoaderListener</listener-class>
</listener>
又如: --->自定义context-param,且自定义listener来获取这些信息
<context-param>
<param-name>urlrewrite</param-name>
<param-value>fal</param-value>
</context-param>
<context-param>
<param-name>cluster</param-name>
<param-value>fal</param-value>
</context-param>
<context-param>
<param-name>rvletmapping</param-name>
<param-value>*.bbscs</param-value>
</context-param>
<context-param>
<param-name>poststoragemode</param-name>
<param-value>1</param-value>伤感情歌大全100首随机播放
</context-param>
<listener>
<listener-class>com.laoer.bbscs.web.rvlet.SysListener</listener-class>
</listener>
public class SysListener extends HttpServlet implements ServletContextListener {
private static final Log logger = Log(SysListener.class);
public void contextDestroyed(ServletContextEvent sce) {
//用于在容器关闭时,操作
}
//用于在容器开启时,操作
public void contextInitialized(ServletContextEvent sce) {
String rootpath = ServletContext().getRealPath("/");
System.out.println("-------------rootPath:"+rootpath);法国号教案
if (rootpath != null) {
rootpath = placeAll("\\\\", "/");
} el {
rootpath = "/";
}
if (!dsWith("/")) {
rootpath = rootpath + "/";
}
Constant.ROOTPATH = rootpath;
logger.info("Application Run Path:" + rootpath);
String urlrewrtie =励志群名 ServletContext().getInitParameter("urlrewrite");
boolean burlrewrtie = fal;
if (urlrewrtie != null) {
burlrewrtie = Boolean.parBoolean(urlrewrtie);
}
Constant.USE_URL_REWRITE = burlrewrtie;
logger.info("U Urlrewrite:" + burlrewrtie);
其它略之....
}
}
/*最终输出
-------------rootPath:D:\tomcat_bbs\webapps\BBSCS_8_0_3\
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.rvlet.SysListener]-[INFO]
论成语
Application Run Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.rvlet.SysListener]-[INFO]
U Urlrewrite:true
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.rvlet.SysListener]-[INFO]
U Cluster:fal
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.rvlet.SysListener]-[INFO]
SERVLET MAPPING:*.bbscs
2009-06-09 21:51:46,573 [com.laoer.bbscs.web.rvlet.SysListener]-[INFO]
Post Storage Mode:1
*/
context-param和init-param区别
l里面可以定义两种参数:
(1)application范围内的参数,存放在rvletcontext中,在l中配置如下:
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
(2)rvlet范围内的参数,只能在rvlet的init()方法中取得,在l中配置如下:
<rvlet>
<rvlet-name>MainServlet</rvlet-name>
<rvlet-class>ller.MainServlet</rvlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in rvlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</rvlet>
在rvlet中可以通过代码分别取用:
package ller;
import javax.rvlet.ServletException;
import javax.rvlet.http.HttpServlet;
public class MainServlet extends HttpServlet ...{
public MainServlet() ...{
super();
}
public void init() throws ServletException ...{
租房合同协议书 System.out.println("下面的两个参数param1是在rvlet中存放的");
System.out.InitParameter("param1"));
System.out.println("下面的参数是存放在rvletcontext中的");
东河流 System.out.println(getServletContext().getInitParameter("context/param"));
}
}