Link: http://www.javabeat.net/tips/178-servletcontextlistener-example.html
This tips explains the how to use ServletContextListener
. There will be only one ServletContext for each web application. ServletContext will be created while deploying the application. Once the ServletContext is created, it will be used by all the servlets and jsp files in the same application. ServletContext
is also called as the application scope variables in the web application scenario.
ServletContextListener
has the following two methods:
-
- public void contextInitialized(ServletContextEvent event)
-
- public void contextDestroyed(ServletContextEvent event)
Look into the following example for how to implement the ServletContextListener methods:
ServletContextExample.java
package example;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ServletContextExample implements ServletContextListener{
ServletContext context;
public void contextInitialized(ServletContextEvent contextEvent) {
System.out.println("Context Created");
context = contextEvent.getServletContext();
// set variable to servlet context
context.setAttribute("TEST", "TEST_VALUE");
}
public void contextDestroyed(ServletContextEvent contextEvent) {
context = contextEvent.getServletContext();
System.out.println("Context Destroyed");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>
example.ServletContextExample
</listener-class>
</listener>
</web-app>
In the above example ServletContextExample
implements ServletContextListener
. It has two methods contextInitialized
which is called when the ServletContext is created. Another one is contextDestroyed
which is called while context is destroyed, this happens normally when web server is shut down or crash.
分享到:
相关推荐
ServletContextListener使用详解
通过创建ServletContextListener监听器,socket来实现tcp/ip协议客户端数据的接收功能。这是一个maven项目,直接导入eclipse即可运行,此处监听的是9092端口,将项目运行起后,可以对其端口使用情况进行查看,在...
NULL 博文链接:https://chenhua-1984.iteye.com/blog/374683
ServletContextListener的应用案例 项目启动:创建新的空表, 项目关闭:删除表 百度六祎,期...
ServletContextListener完成在线人数统计和显示人员列表
在SpringMVC中利用ServletContextListener初始化数据到内存
主要介绍了Java基于ServletContextListener实现UDP监听,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
听众 ServletContextListener,sesson,请求侦听器
JavaWeb新版教程. jsp--ServletContextListener监听器演示.avi
public class Test extends HttpServlet implements ServletContextListener { // 服务器停止时执行该事件 public void contextDestroyed(ServletContextEvent sce) { DeleteFile();// 删除启动服务器时建立的...
编写程序,提供用户登录界面。 登录成功后,提示用户上次登录时间,登录IP地址信息。 保存用户的登录信息可以使用XML,也可以使用纯文本文件。... 要求使用ServletContextListener,要求理解Listener.
介绍了Java定时执行任务,给出了如何使用ServletContextListener实现定时运行java某个方法的例子,简单易用。
5)ServletContextListener监听器的主要作用 a、初始化的工作:初始化对象 初始化数据 ---- 加载数据库驱动 连接池的初始 化 b、加载一些初始化的配置文件 --- spring的配置文件 c、任务调度----定时器----Timer/...
public class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { log.info("web项目的启动{} 一开始就启动",sce.getServletContext...
在启动时,它使用 ServletContextListener 创建一个 websocket 连接。 它将类 WebSocketClient 作为 ClientEndpoint 传递。 ClientEndpoint 处理所有传入事件。 连接顺序 随着 WAR 的部署,一个 ...
classloader-leak-prevention.zip,防止类加载器泄漏的servletcontextlistener/java.lang.outofmemory错误:permgen spaceclassloader泄漏预防/保护
public class BookListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } public void contextInitialized...
企业drp系统经典实现。 包括: v1.0 * 用户添加 v1.1 * 采用Ajax判断用户代码是否重复 ... * ServletContextListener v3.1 * JfreeChart的应用 v3.2 * 采用Ajax实现下拉列表的联动 v3.3 * 验证码的使用
ServletContextListener ServletException ServletInputStream ServletOutputStream ServletRequest ServletRequestAttributeEvent ServletRequestAttributeListener ServletRequestEvent ...