<context-param>与<init-param>
<context-param>的作用:
web.xml的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
context-param的值 = ServletContext.getInitParameter("context-param的键");
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
<!-- 加载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>
<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("\\\\", "/");
} 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_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>
<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>
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的init()方法中通过this.getInitParameter("param1")取得.
<context-param>与<init-param>的更多相关文章
- button与input[type=”button”]的区别
button与input[type="button"]的区别 特别感谢 @守护晴天 ,指出了博客中不细致不严谨的地方,也让我学到了更多,更多是觉得抱歉,由于自己的不细致可能误导了一 ...
- 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别
这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...
- <button>和<input type="button"> 的区别
<button>标签 定义和用法 <button> 标签定义一个按钮. 在 button 元素内部,您可以放置内容,比如文本或图像.这是该元素与使用 input 元素创建的按钮 ...
- <button>与<input type="button"> 的区别
<button> button按钮点击会刷新整个页面 <input type="button"> 不会刷新整个页面 本文为本人用来记录自己做的一些东西,如 ...
- <button>与<input type="button">的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
- 解析<button>和<input type="button"> 的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
- <button>与<input type="button">
在做form表单,点击按钮随机生成两串密钥的时候 1.用第一种按钮的时候,会出现刷新form表单的现象.会把创建密钥前面的输入框中的字消失.虽然能生成密钥1和密钥2,但是会闪一下,随即消失.几个输入框 ...
- 解析button和input type=”button”的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
- 【转】解析<button>和<input type="button"> 的区别
一.定义和用法 <button> 标签定义的是一个按钮. 在 button 元素内部,可以放置文本或图像.这是<button>与使用 input 元素创建的按钮的不同之处. 二 ...
- button和input type=button的区别及注意事项
<button>标签 定义和用法 <button>标签定义一个按钮. 在button元素内部,您可以放置内容,比如文本或图像.这是该元素与使用input元素创建的按钮之间的不同 ...
随机推荐
- 使用 Flexbox 的居中布局
- Ubuntu 14 添加Windows风格的底部任务栏
习惯了Windows风格的底部任务栏,而Ubuntu 14是没有的,还好有人做好了一个任务栏插件,可以在线安装: 1.打开终端(Ctrl+Alt+T),然后输入下面的命令 sudo apt-get i ...
- W3Cschool菜鸟教程离线版下载链接
请在电脑上打开以下链接进行下载w3cschool 离线版(chm):http://pan.baidu.com/s/1bniwRCV(最新,2014年10月21日更新)w3cschool 离线版(htm ...
- 理解js中this的指向
学习自原文 http://www.cnblogs.com/pssp/p/5216085.html后的一点小结(原文作者总结的很棒^_^)! 关于js中this的指向,在函数定义的时候还无法 ...
- tab切换,滑动门
<SCRIPT type=text/javascript> jQuery(document).ready(function () { changediv([" ...
- java读取文件
一个字节一个字节地读取 File file = InputStream inputStream = ...
- Qt5 程序启动画面动图效果
2333终于实现动图,先弄了一个窗口去掉标题栏假装就是启动画面了,还是那只萌萌的猫这次会动了! 基类用的是QWidget 类名称MainView #ifndef MAINVIEW_H #define ...
- OS X 添加环境变量
这个方法相对靠谱,可用: 1, cd 到home 2, touch .bash_profile 3,open -e .bash_profile 把各个路径按如下填写: export PATH=${PA ...
- openstacksdk enable logging
http://git.openstack.org/cgit/openstack/python-openstacksdk/tree/doc/source/users/guides/logging.rst
- DataTable得到某行某列的值
DataTable dt=this.GetRepeatTableData("repeating1"); int count=dt.Rows.Count;for(int x=0;x& ...