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(ServletContextEventargs)初始化方法,在这个方法中获得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>
publicclass SysListenerextends HttpServlet implements ServletContextListener {
privatestatic final Log logger = LogFactory.getLog(SysListener.class);
publicvoidcontextDestroyed(ServletContextEventsce) {
//用于在容器关闭时,操作
}
//用于在容器开启时,操作
publicvoidcontextInitialized(ServletContextEventsce) {
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]
ApplicationRun Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/
2009-06-09 21:51:46,526[com.laoer.bbscs.web.servlet.SysListener]-[INFO]
UseUrlrewrite:true
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]
UseCluster:false
2009-06-09 21:51:46,526[com.laoer.bbscs.web.servlet.SysListener]-[INFO]
SERVLETMAPPING:*.bbscs
2009-06-09 21:51:46,573[com.laoer.bbscs.web.servlet.SysListener]-[INFO]
PostStorage 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 servletinit()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
在servlet中可以通过代码分别取用:
package com.wes.controller;
importjavax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
publicclass 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")取得.
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(ServletContextEventargs)初始化方法,在这个方法中获得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>
publicclass SysListenerextends HttpServlet implements ServletContextListener {
privatestatic final Log logger = LogFactory.getLog(SysListener.class);
publicvoidcontextDestroyed(ServletContextEventsce) {
//用于在容器关闭时,操作
}
//用于在容器开启时,操作
publicvoidcontextInitialized(ServletContextEventsce) {
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]
ApplicationRun Path:D:/tomcat_bbs/webapps/BBSCS_8_0_3/
2009-06-09 21:51:46,526[com.laoer.bbscs.web.servlet.SysListener]-[INFO]
UseUrlrewrite:true
2009-06-09 21:51:46,526 [com.laoer.bbscs.web.servlet.SysListener]-[INFO]
UseCluster:false
2009-06-09 21:51:46,526[com.laoer.bbscs.web.servlet.SysListener]-[INFO]
SERVLETMAPPING:*.bbscs
2009-06-09 21:51:46,573[com.laoer.bbscs.web.servlet.SysListener]-[INFO]
PostStorage 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 servletinit()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
在servlet中可以通过代码分别取用:
package com.wes.controller;
importjavax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
publicclass 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")取得.
发表评论
-
MAC OS 配置svn服务器
2015-07-27 16:07 736MAC如果安装了XCode,就已经自带了SVN,所以,直接使 ... -
tomcat下部署了多个项目且都使用log4j
2015-07-24 11:33 847<!--如果不定义webAppRootKey参数,那么 ... -
Eclipse里项目名有红叉,但是底下的每一个文件都没有红叉
2015-07-04 17:43 778下面每个单独的文件都没有红叉报错,整个项目上显示个红叉; ... -
maven中导入本地jar包
2015-07-03 15:57 1075要在maven中引入本地jar包,首先需要将jar包复制到项 ... -
java 执行sql脚本的3种方式 (ant,ibatis,ScriptRunner) (转)
2015-01-21 10:10 1125java执行sql文件这个问题麻烦的地方在于解析sql ... -
spring mvc 地址栏 中文传值显示乱码解决办法
2014-09-04 10:29 673修改tomcat的server.xml文件,将<Con ... -
java.sql.SQLException: Value'0000-00-00'异常解决办法及mysql的url设置(转)
2014-04-10 11:47 739一、0000-00-00错误及 ... -
hibernate配置
2014-01-07 10:19 0<prop key="hibernate.c ... -
windows下一台机器安装两个MYSQL数据库
2013-12-27 16:20 7911.正常安装第一个mysql(安装步骤省略) 2.在控制面板 ... -
服务器mysql及c3p0设置
2013-12-19 14:20 7041)interactive_timeout: 参数含义:服务器 ... -
hibernate关闭连接
2013-12-17 17:02 815通过session工厂关闭session对象。 将sessio ... -
web.xml 配置 mime-mapping
2013-12-17 15:56 14331.指定了自己的javaEncoding <servl ... -
JSP中fckeditor控件表情修改以及文件上传(转)
2013-10-07 16:58 953(一)fckeditor控件表情修改 fckconfig.j ... -
struts2和fck冲突造成无法上传图片或文件问题解决办法
2013-10-07 16:08 744struts2和fck冲突造成无法 ...
相关推荐
`<context-param>` 和 `<init-param>` 的主要区别在于作用范围和目标对象。`<context-param>` 作用于整个Web应用,影响Spring的ApplicationContext;而 `<init-param>` 则是针对特定Servlet或Filter的个性化配置。...
<param-name>context/param</param-name> <param-value>avalible during application</param-value> </context-param> ``` 在这里,`<param-name>`标签定义了参数名,`<param-value>`标签定义了参数值。要从...
class><br> <init-param><br> <param-name>baseDir</param-name><br> <param-value>/ userDir</param-value><br> </init-param><br> <init-param><br> <param-name>debug</param-name><br> <param-value...
</init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 最后,启动你的Web应用...
<param-value>default.context</param-value> </context-param> ``` 5. **log4jConfigLocation**:指定日志配置文件的位置。 ```xml <context-param> <param-name>log4jConfigLocation</param-name> <param...
<init-param> <param-name>casServerUrlPrefix</param-name> <param-value>http://gis.hayao.com/hayao_cas2</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>...
<context-param> <param-name>casServerLogoutUrl</param-name> <param-value>http://192.168.156.120:8080/cas/logout</param-value><!--server cas 地址--> </context-param> <!-- 用于单点退出,该...
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/database.xml /WEB-INF/applicationContext.xml </param-value> </context-param> <listener> <listener-...
这些参数都可以在`<init-param>`标签中设置,以达到细微调整代理转发行为的目的。例如: ```xml <init-param> <param-name>PathTrim</param-name> <param-value>/proxy</param-value> </init-param> <init-param>...
<init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> ... </servlet> ``` 2. **Spring配置**: - `applicationContext-*.xml`:Spring的...
该元素包含多个子元素,包括`<filter-name>`、`<filter-class>`、`<init-param>`等。 其中,`<filter-name>`元素用于定义过滤器的名称,该名称在整个应用程序中必须是惟一的。`<filter-class>`元素用于指定过滤器类...
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.spring...
<context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --> <param-value>classpath:...
</context-param> <!-- 字符编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> ...
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> ...
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-...