web.xml中的ServletContextListener
要想了解ServletContextListener,先看看web.xml中的<listener>配置。
一)web.xml中的内容载入顺序:
首先能够肯定的是,载入顺序与它们在 web.xml 文件里的先后顺序无关。
即不会由于 filter 写在 listener 的前面而会先载入 filter。终于得出的结论是:listener -> filter -> servlet
同一时候还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任何位置,因此真正的载入顺序为:context-param -> listener -> filter -> servlet
对于某类配置节而言,与它们出现的顺序是有关的。
以 filter 为例。web.xml 中当然能够定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有同样 filter-name 的 filter 和 filter-mapping 配置节而言。filter-mapping 必须出如今 filter 之后。否则当解析到 filter-mapping 时。它所相应的 filter-name 还没有定义。web 容器启动时初始化每一个 filter
时,是依照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是依照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。
servlet 同 filter 类似 ,此处不再赘述。
由此,能够看出,web.xml 的载入顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是依据相应的 mapping 的顺序进行调用的。
二)ServletContextListener 是什么:从上面载入顺序分享一下,ServletContextListener 就是一个监听器。
ServletContext 被 Servlet 程序用来与 Web 容器通信。
比如写日志,转发请求。每个 Web 应用程序含有一个Context,被Web应用内的各个程序共享。
由于Context能够用来保存资源而且共享,所以我所知道的 ServletContext 的最大应用是Web缓存----把不常常更改的内容读入内存。所以server响应请求的时候就不须要进行慢速的磁盘I/O了。
ServletContextListener 是 ServletContext 的监听者,假设 ServletContext 发生变化,如server启动时 ServletContext 被创建,server关闭时 ServletContext 将要被销毁。
在JSP文件里。application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。
三)ServletContextListener 可以做些什么:一切你想做的,就是一个监听器可以做的事。
1.设计缓存:
思路:
缓存的思路大概是:
1)server启动时,ServletContextListener 的 contextInitialized()方法被调用,所以在里面创建好缓存。能够从文件里或者从数据库中读取取缓存内容生成类,用 ervletContext.setAttribute()方法将缓存类保存在 ServletContext 的实例中。
2)程序使用 ServletContext.getAttribute()读取缓存。
假设是 JSP。使用a pplication.getAttribute()。假设是 Servlet。使用 getServletContext().getAttribute()。
假设缓存发生变化(如訪问计数),你能够同一时候更改缓存和文件/数据库。
或者你等 变化积累到一定程序再保存。也能够在下一步保存。
3)server将要关闭时,ServletContextListener 的 contextDestroyed()方法被调用,所以在里面保存缓存的更改。将更改后的缓存保存回文件或者数据库,更新原来的内容。
import User; //my own class
import DatabaseManager; // my own class
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener; public class MyContextListener implements ServletContextListener {
private ServletContext context = null; public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
User user = DatabaseManager.getUserById(1);
context.setAttribute("user1", user);
} public void contextDestroyed(ServletContextEvent event) {
User user = (User)context.getAttribute("user1");
DatabaseManager.updateUserData(user);
this.context = null;
}
}
2)推断一个配置文件的编码格式:
public class InitProperties implements ServletContextListener { private static final Logger logger = Logger
.getLogger(InitProperties.class); @Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub }
/**
* 该监听器仅仅用于初始化时监听配置文件是否是utf-8编码 且仅仅在初始化时运行一次 对兴许的各种请求无影响
* **/
@Override
public void contextInitialized(ServletContextEvent arg0) { //载入文件的编码格式 查看是否是utf-8类型的
try {
String fileUrl = PropertiesHander.class.getResource("/evoucher.conf").toString();
fileUrl = fileUrl.replace("file:",""); File file = new File(fileUrl);
InputStream ios = new java.io.FileInputStream(file);
byte[] b = new byte[3]; try {
ios.read(b);
ios.close();
} catch (IOException e) { logger.error("检測编码载入时关闭流出现载入异常");
} //默认编码是 23 23 23 的 相同能够载入这样的默认的编码
if (b[0] == 35 && b[1] == 35 && b[2] == 35) {
logger.info(file.getName() + ":编码为默认编码");
}
//utf-8编码的首字母是 ef bb bf
else if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
logger.info(file.getName() + ":编码为UTF-8");
} else
{
try{
// logger.error(file.getName() + "文件编码格式不是UTF-8编码。请又一次确定编码格式!");
throw new EVoucherException(file.getName() + "文件编码格式不是UTF-8编码,请又一次确定编码格式!");
}
catch(EVoucherException e){
// logger.error(e);
EVoucherException.getEVException(null, e, logger);
System.exit(0); } } } catch (FileNotFoundException e) { logger.error("检測编码时载入文件出现异常"); } } }
四)布署 ServletContextListener
<listener>
<listener-class>MyServletContextListener</listener-class>
</listener>
<!-- 自己主动监听配置文件是否是utf-8编码 -->
<listener>
<listener-class>assp.evoucher.common.util.InitProperties</listener-class>
</listener>
web.xml中的ServletContextListener的更多相关文章
- JavaEE web.xml 中ContextLoaderListener的解析
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...
- web.xml中listener作用及使用
一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...
- web.xml中常用元素的解读
前言 针对一个项目而言,通常会有几类XML文件需要书写. web.xml spring-context.xml spring-mvc.xml other.xml ... 不管有多少配置文件,可以肯定的 ...
- 【转】web.xml中的contextConfigLocation在spring中的作用
一.spring中如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,sp ...
- 如何把web.xml中的context-param、Servlet、Listener和Filter定义添加到SpringBoot中
把传统的web项目迁移到SpringBoot中,少不了web.xml中的context-param.Servlet.Filter和Listener等定义的迁移. 对于Servlet.Filter和Li ...
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...
- (转)web.xml中的contextConfigLocation在spring中的作用
(转)web.xml中的contextConfigLocation在spring中的作用 一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...
- 在web.xml中配置监听器来控制ioc容器生命周期
5.整合关键-在web.xml中配置监听器来控制ioc容器生命周期 原因: 1.配置的组件太多,需保障单实例 2.项目停止后,ioc容器也需要关掉,降低对内存资源的占用. 项目启动创建容器,项目停止销 ...
- web.xml中Filter,Listener,Servlet的区别
一.Servlet Servlet是基本的服务端程序,他来自接口Servlet,接口中有方法service.而Servlet的一个重要实现类,则是tomcat服务器的核心,那就是HttpServlet ...
随机推荐
- python TCP协议详解 三次握手四次挥手和11种状态
11种状态解析 LISTEN -------------------- 等待从任何远端TCP 和端口的连接请求. SYN_SENT --------------- 发送完一个连接请求后等待一个 ...
- 条款14:在资源管理类中心copying行为(Think carefully about copying behavior in resource-manage classes)
NOTE: 1.复制RAII 对象必须一并赋值它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 2.普遍而常见的RAII class copying 行为是: 抑制c ...
- jenkins在linux环境搭建-公司系统
1.按照这个搭建的项目https://www.cnblogs.com/zishengY/p/7170656.html 2.配置权限https://blog.csdn.net/mynameissls/a ...
- NYOJ-481平衡字符串
平衡字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给你一定长度的字符串.字符串中只包含26个小写字母,首先我们把字母a-z分为2堆(a--m)和(n--z),判 ...
- HDU-2509-Be the Winner,博弈题~~水过~~
Be the Winner Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ht ...
- 2017"百度之星"程序设计大赛 - 初赛(A)数据分割
n<=100000条相等/不等关系描述<=100000个数,把这些数据分割成若干段使得每一段描述都出现冲突且冲突只出现在最后一行. 相等关系具有传递性,并查集维护:不等关系根据相等关系进行 ...
- redis哨兵模式配置
java对redis的读写 依赖包:jedis.jar maven下: <!-- https://mvnrepository.com/artifact/redis.clients/jedis - ...
- gerrit ssh 登陆设置
[root@web ~]# cat ~/.ssh/config Host gerrit User deploy-gerrit Port Hostname gerrit.demo.com Identit ...
- Google SPDY
SPDY(读作“SPeeDY”)是Google开发的基于TCP的应用层协议,用以最小化网络延迟,提升网络速度,优化用户的网络使用体验.SPDY并不是一种用于替代HTTP的协议,而是对HTTP协议的增强 ...
- python学习之-- socketserver模块
socketserver 模块简化了网络服务器的编写,主要实现并发的处理. 主要有4个类:这4个类是同步进行处理的,另外通过ForkingMixIn和ThreadingMixIn类来支持异步.sock ...