[转] <context-param>与<init-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>
- 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>
(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")取得.
[转] <context-param>与<init-param>的区别与作用的更多相关文章
- git init 与 git init --bare 的区别
git init 和 git init –bare 的区别 使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repos ...
- JVM思考-init和clinit区别
JVM思考-init和clinit区别 目录:JVM总括:目录 clinit和init的区别其实也就是Class对象初始化对象初始化的区别,详情看我上一篇博客: JVM总括四-类加载过程.双亲委派模型 ...
- (转)Linux下/etc/rc.local与/etc/init.d的区别与联系
Linux下/etc/rc.local与/etc/init.d的区别与联系 2012-10-13 20:14:52| 分类: Linux学习|字号 订阅 1./etc/rc.local 这是 ...
- 深入理解jvm--Java中init和clinit区别完全解析(转)
转自:http://blog.csdn.net/u013309870/article/details/72975536 init和clinit区别 ①init和clinit方法执行时机不同 init是 ...
- weex 项目开发(一) weex create project 与 weex init project 的区别
开发环境配置:http://www.cnblogs.com/crazycode2/p/7822961.html 1. weex create project 与 weex init project ...
- git init和git init –bare的区别:
感谢原文作者:ljchlx 原文链接:https://blog.csdn.net/ljchlx/article/details/21805231 git init 和 git init –bare 的 ...
- 浅析匿名函数、lambda表达式、闭包(closure)区别与作用
浅析匿名函数.lambda表达式.闭包(closure)区别与作用 所有的主流编程语言都对函数式编程有支持,比如c++11.python和java中有lambda表达式.lua和JavaScript中 ...
- maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令
maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令 在日常的工作中由于各种原因,会出现这样一种情况,某些项目并没有打包至mvnrepository. ...
- 深入解析Oracle 10g中SGA_MAX_SIZE和SGA_TARGET参数的区别和作用
原文链接:http://m.blog.csdn.net/blog/aaron8219/40037005 SGA_MAX_SIZE是从9i以来就有的作为设置SGA大小的一个参数,而SGA_TARGET则 ...
随机推荐
- linux非阻塞的socket EAGAIN的错误处理【转】
转自:http://blog.csdn.net/tianmohust/article/details/8691644 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux中使用非阻塞的s ...
- 64_l2
libcec-devel-4.0.2-3.fc26.x86_64.rpm 22-May-2017 22:13 27942 libcephfs-devel-10.2.7-2.fc26.i686.rpm ...
- OPENSOLARIS source
http://blog.csdn.net/nemo2011/article/details/8543220 http://fxr.watson.org/fxr/source/?v=OPENSOLARI ...
- Redis 分片实现 Redis Shard [www]
Redis 分片实现 Redis Shard https://www.oschina.net/p/redis-s ...
- Makefile 跟着走快点
引言 - 从"HelloWorld"开始 Makefile 是Linux C 程序开发最重要的基本功. 代表着整个项目编译和最终生成过程.本文重点是带大家了解真实项目中那些简易的 ...
- Makefile系列之一 : 书写规则
1. 规则 target : prerequisites command 2. example excute 为最终生成的可执行文件. 可以通过命令 make clean来删除所有编译时产生的中间文 ...
- Python在线教程
Python 3.x的 http://www.ziqiangxuetang.com/python3/python3-stdlib.html 廖雪峰的官方网站 http://www.liaoxuefen ...
- linux命令(38):traceroute命令
1.命令格式: traceroute[参数][主机] 2.命令功能: traceroute指令让你追踪网络数据包的路由途径,预设数据包大小是40Bytes,用户可另行设置. 具体参数格式:tracer ...
- poj 3264(RMQ或者线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42929 Accepted: 20184 ...
- Python3通过汉字输出拼音
https://github.com/mozillazg/python-pinyin # pip install pypinyin from pypinyin import pinyin, lazy_ ...