ServletContextListener的作用
ServletContextListener是对ServeltContext的一个监听.servelt容器启动,serveltContextListener就会调用contextInitialized方法.在方法里面调用event.getServletContext()可以获取ServletContext,ServeltContext是一个上下文对象,他的数据供所有的应用程序共享,进行一些业务的初始化servelt容器关闭,serveltContextListener就会调用contextDestroyed.
实际上ServeltContextListener是生成ServeltContext对象之后调用的.生成ServeltContext对象之后,这些代码在我们业务实现之前就写好,它怎么知道我们生成类的名字.实际上它并不需要知道我们的类名,类里面有是方法.他们提供一个规范,就一个接口,ServeltContextListner,只要继承这个接口就必须实现这个方法.然后这个类在web.xml中Listener节点配置好.Servelt容器先解析web.xml,获取Listener的值.通过反射生成对象放进缓存.然后创建ServeltContext对象和ServletContextEvent对象.然后在调用ServletContextListener的contextInitialized方法,然后方法可以把用户的业务需求写进去.struts和spring框架就是类似这样的实现,我们以后写一些框架也可以在用.
java代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.chen;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyServletContextListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("destroyed"); } @Override public void contextInitialized(ServletContextEvent event) { System.out.println("initialized"); event.getServletContext().setAttribute("user", "admin"); }}<br> |
xml配置
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>oa</display-name> <listener> <listener-class>com.chen.MyServletContextListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app> |
ServletContextListener的作用的更多相关文章
- Web中Listener的创建
使用Listener只需要两个步骤: 定义Listener实现类. 通过Annotation或在web.xml文件中配置Listener 实现Listener类 监听不同Web事件的监听器不相同,常用 ...
- listener介绍
当Web 应用在Web 容器中运行时, Web 应用内部会不断地发生各种事件: 如Web 应用被启动.Web 应用被停止,用户session 开始.用户session 结束.用户请求到达等, 通常来说 ...
- JavaWeb日常笔记
1. XML文档的作用和解析 1. XML的基本概述: XML的主要是用来存储一对多的数据,另外还可以用来当做配置文件存储数据.XML的表头如下: <?xml version='1.0' e ...
- Spring MVC入门的实例
作为Spring MVC入门,以XML配置的方式为例.首先需要配置Web工程的web.xml文件. 代码清单14-1:web.xml配置Spring MVC <?xml version=&q ...
- JavaWeb学习笔记(六)jsp
第六章.jsp 1.什么是jsp jsp:java server pages,java的服务器页面 作用:代替Servlet回传HTML页面的数据 因为Servlet程序回传HTML页面的数据很繁琐, ...
- ServletContextListener作用(转)
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...
- ServletContextListener作用
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...
- ServletContextListener使用详解
在 Servlet API 中有一个 ServletContextListener 接口,它能够监听 ServletContext 对象的生命周期,实际上就是监听 Web 应用的生命周期. 当Serv ...
- ContextLoaderListener作用详解(转)
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...
随机推荐
- asp.net core webapi 生成导出excel
/// <summary> /// 下载订单 /// </summary> /// <param name="model"></param ...
- Spring.net的一些感悟
模拟Oracle+spring.net+NHibernate+MVC 对DLL文件引用问题得注意,库文件最好放在固定目录,否则严重影响项目迁移的成功, 由此可见,“代码规范”的重要性(自己大部分时间耽 ...
- .Net Core 读取配置文件 appsettings.json
1. 首先些一个类 public class MySettings { public string P1 { get; set; } public string P2 { get; set; } } ...
- ABP框架 sql语句(转载)
ABP.Core实现SQL语句仓储,支持EF.Core兼容的数据库 来源:https://blog.csdn.net/qq_28699537/article/details/80522680?tds ...
- 什么是 Native、Web App、Hybrid、React Native 和 Weex?(转载)
什么是 Native.Web App.Hybrid.React Native 和 Weex? 来源:zwwill_木羽 segmentfault.com/a/1190000011154120 一句 ...
- C# 未能加载文件或程序集“ Newtonsoft.Json” Json格式错误
原因:版本不一致,所使用的dll和配置文件中的版本不一致.解决: (1)查看所使用的Newtonsoft.Json.dll版本 ,然后把对应的版本修改在配置文件中如下,比如版本为“4.5.0.0” 修 ...
- Spring Boot Docker 实战
Spring Boot Docker 开发环境 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_192 maven: ...
- kafka消费者启动报错
报错信息: WARN Fetching topic metadata with correlation id 14 for topics [Set(test)] from broker [Broker ...
- js循环json得到 键和值
var jsondata=[{"男":4,"女":3,"不详":0},{"男one":23,"女two&quo ...
- laravel接值 get post
laravel使用一种简单的方式来访问用户提交的信息. 你可以用统一的方式来访问用户提交的信息,而不用为用户提交信息的方式操心. 引用类:use Illuminate\Support\Facades\ ...