博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
<init-param>和<context-param>两个标签的区别
阅读量:7181 次
发布时间:2019-06-29

本文共 3424 字,大约阅读时间需要 11 分钟。

hot3.png

问题:在看项目的web.xml时,发现这两个标签,且,标签中的内容有重合部门,但是读取的对应的xml文件不同。

contract
contextConfigLocation
classpath:applicationContext.xml classpath:spring-security.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
org.springframework.web.util.IntrospectorCleanupListener
org.springframework.web.util.Log4jConfigListener
contract
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml classpath:spring-quartz.xml
1
true
contract
/
httpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
httpMethodFilter
contract
xssFilter
com.contract.base.mvc.filter.XssFilter
xssFilter
/*
REQUEST
540
COOKIE
false
true

首先我来来解释一个web.xml中配置<context-param>作用: 1.启动一个web项目的额时候,容器(如:tomcat)回去读它的配置文件web.xml。读两个点:<listener></listener> 和<context-param></context-param> 2.紧接着,容器创建一个ServletContext(上下文),这个项目所有的部分都讲共享这个上下文。 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.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.

(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()方法中取得

转载于:https://my.oschina.net/u/3045515/blog/1586882

你可能感兴趣的文章
海量高性能列式数据库HiStore技术架构解析
查看>>
Linux块设备驱动之内存模拟块设备
查看>>
「技术大牛」是如何缩短事件平均解决时间的?
查看>>
新人成长:新人如何快速融入技术实力强的前端团队
查看>>
Testing Flutter apps翻译-性能分析
查看>>
手把手教你用 node 玩跳一跳
查看>>
SQL 优化
查看>>
如何在SpringBoot中集成JWT(JSON Web Token)鉴权
查看>>
Redis应用场景及常见问题
查看>>
Sass初入门
查看>>
js常见算法(一):数组去重,打乱数组,统计数组各个元素出现的次数, 字符串各个字符的出现次数,获取地址链接的各个参数...
查看>>
lua 学习总结
查看>>
spring+Kafka+springmvc Demo
查看>>
基于Docker下的MySQL主从复制
查看>>
VUE 面试总结
查看>>
React Native组件开发指南
查看>>
keep-alive:组件级缓存
查看>>
flex 布局基本要点
查看>>
TextWatcher的使用及源码解析
查看>>
linux ssh 免密登录
查看>>