<context-param>与<init-param>的差别与感化
添加时间:2013-5-9 点击量:
转自:http://www.cnblogs.com/hzj-/articles/1689
<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(\\\\, /);
}
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)取得.
应用ServletContextListener接口,可以实如今web应用法度初始化时,主动运行一些初始化法度。ServletContextListener接口定义的办法
应用此接口时,要在web.xml文件内定义“器类”的名称,此时要重视:在Servlet规范中并未限制一个Web应用法度只能对应一个“器类”,然则在web.xml内定义<listener>元素时得重视下列两点:
<listener>元素必须呈如今任何Context肇端参数(由<context-param>元素所定义)之后。
<listener>元素必须呈如今任何Servlet实体(由<servlet>元素所定义)之前。
我所有的自负皆来自我的自卑,所有的英雄气概都来自于我的软弱。嘴里振振有词是因为心里满是怀疑,深情是因为痛恨自己无情。这世界没有一件事情是虚空而生的,站在光里,背后就会有阴影,这深夜里一片寂静,是因为你还没有听见声音。—— 马良《坦白书》
转自:http://www.cnblogs.com/hzj-/articles/1689
<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的键);
context-param的值 = ServletContext.getInitParameter(context-param的键);
6.获得这个context-param的值之后,你就可以做一些操纵了.重视,这个时辰你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时辰,你对<context-param>中的键值做的操纵,将在你的WEB项目完全启动之前被履行.
换句话说,这个时辰,你对<context-param>中的键值做的操纵,将在你的WEB项目完全启动之前被履行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接体式格式,在类中初始化数据库的连接.
那么这里就可以在<context-param>中设置数据库的连接体式格式,在类中初始化数据库的连接.
8.这个是本身写的一个类,除了初始化办法,它还有烧毁办法.用于封闭应用前开释资料.比如说数据库连接的封闭.
如:
<!-- 加载spring的设备文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-
<!-- 加载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>
<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>
<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);
String rootpath = sce.getServletContext().getRealPath(/);
System.out.println(-------------rootPath:+rootpath);
if (rootpath != null) {
rootpath = rootpath.replaceAll(\\\\, /);
}
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 = rootpath.replaceAll(\\\\, /);
}
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]
/终极输出
-------------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]
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]
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]
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]
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>
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>
<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;
package com.wes.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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));
}
}
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)取得.
第二种参数只能在servlet的init()办法中经由过程this.getInitParameter(param1)取得.
应用ServletContextListener接口,可以实如今web应用法度初始化时,主动运行一些初始化法度。ServletContextListener接口定义的办法
应用此接口时,要在web.xml文件内定义“器类”的名称,此时要重视:在Servlet规范中并未限制一个Web应用法度只能对应一个“器类”,然则在web.xml内定义<listener>元素时得重视下列两点:
<listener>元素必须呈如今任何Context肇端参数(由<context-param>元素所定义)之后。
<listener>元素必须呈如今任何Servlet实体(由<servlet>元素所定义)之前。