web.xml在listener作用与用途
一.WebContextLoaderListener 监听类
它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作!
它必须在web.xml 中配置才干使用,是配置监听类的
二.以下是搜集的一些listener方面的知识
简例一
监听用户上线与退出,显示在线用户
1、登陆页面 Login.jsp
<%@page pageEncoding="gb2312" contentType="text/html; charset=gb2312" %>
<%
session=request.getSession(false);
if(session!=null)session.invalidate();
%>
<html>
<head><title></title></head>
<body>
<form action="isOnline.jsp" method="post">
username:<input type="text" name="uName"/>
<input type="submit" value="上线">
</form>
</body>
</html>
2、控制页面(仅仅是为了说明监听器问题,所以简单了点...) isOnline.jsp
<%@page pageEncoding="gb2312" contentType="text/html; charset=gb2312" %>
<html>
<head><title></title></head>
<body>
<%
session=request.getSession();
session.setAttribute("userName",request.getParameter("uName"));
response.sendRedirect("showOnline.jsp");
%>
</body>
</html>
3、显示页面 showOnline.jsp
<%@page pageEncoding="gb2312" contentType="text/html; charset=gb2312" import="java.util.ArrayList" %>
<html>
<head><title></title></head>
<body>
<%
ArrayList showList=(ArrayList)(getServletContext().getAttribute("list"));
out.print("在线人数 "+showList.size()+"<br>");
for(int i=0;i<showList.size();i++){
out.print(showList.get(i)+"在线"+"<br>");
}
%>
<br>
<a href="Login.jsp">退出</a>
</body>
</html>
4、配置页面 web.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>org.xiosu.listener.onlineListener</listener-class>
</listener>
</web-app>
5、监听器 onlineListener.java
package org.xiosu.listener;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class onlineListener implements HttpSessionListener,
HttpSessionAttributeListener {
// 參数
ServletContext sc;
ArrayList list = new ArrayList();
// 新建一个session时触发此操作
public void sessionCreated(HttpSessionEvent se) {
sc=se.getSession().getServletContext();
System.out.println("新建一个session");
}
// 销毁一个session时触发此操作
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("销毁一个session");
if (!list.isEmpty()) {
list.remove((String) se.getSession().getAttribute("userName"));
sc.setAttribute("list", list);
}
}
// 在session中加入对象时触发此操作,在list中加入一个对象
public void attributeAdded(HttpSessionBindingEvent sbe) {
list.add((String) sbe.getValue());
sc.setAttribute("list", list);
}
// 改动、删除session中加入对象时触发此操作
public void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
}
}
说明:本例仅仅为简介监听器,并未进行安全方面设置。
监听器也叫Listener,是Servlet的监听器,它能够监听client的请求、服务端的操作等。通过监听器,能够自己主动激发一些操作,比方监听在线的用户的数量。当添加一个HttpSession时,就激发sessionCreated(HttpSessionEvent se)方法,这样
就能够给在线人数加1。经常使用的监听接口有下面几个:
ServletContextAttributeListener监听对ServletContext属性的操作,比方添加、删除、改动属性。
ServletContextListener监听ServletContext。当创建ServletContext时,激发 contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session添加一个属性时,激发 attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被又一次设置时,激发attributeReplaced(HttpSessionBindingEvent
se) 方法。
example:随server启动
<web-app>
com.tb.listener.CountStartListener
package com.tb.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import com.tb.timertask.DoCountTask;
public class CountStartListener extends HttpServlet implements ServletContextListener
{
private static final long serialVersionUID = 1824920962239905170L;
public CountStartListener()
{
// TODO Auto-generated constructor stub
}
public void contextDestroyed(ServletContextEvent arg0)
{
// TODO Auto-generated method stub
}
public void contextInitialized(ServletContextEvent arg0)
{
DoCountTask.dotask();
}
}
概述:
Servlet监听器用于监听一些重要事件的发生,监听器对象能够在事情发生前、发生后能够做一些必要的处理。
接口:
眼下Servlet2.4和JSP2.0总共同拥有8个监听器接口和6个Event类,当中HttpSessionAttributeListener与
HttpSessionBindingListener 皆使用HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener相应的Event例如以下所看到的:
Listener接口 |
Event类 |
ServletContextListener |
ServletContextEvent |
ServletContextAttributeListener |
ServletContextAttributeEvent |
HttpSessionListener |
HttpSessionEvent |
HttpSessionActivationListener |
|
HttpSessionAttributeListener |
HttpSessionBindingEvent |
HttpSessionBindingListener |
|
ServletRequestListener |
ServletRequestEvent |
ServletRequestAttributeListener |
ServletRequestAttributeEvent |
分别介绍:
一 ServletContext相关监听接口
补充知识:
通过ServletContext 的实例能够存取应用程序的全局对象以及初始化阶段的变量。
在JSP文件里,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。
注意:
全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由<context-param>元素所设定的变量,它的范围也是Application范围,比如:
<context-param>
<param-name>Name</param-name>
<param-value>browser</param-value>
</context-param>
当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:
String name = (String)application.getInitParameter("Name");
或者使用EL时:
${initPara.name}
若是在Servlet中,取得Name的值方法:
String name = (String)ServletContext.getInitParameter("Name");
1.ServletContextListener:
用于监听WEB 应用启动和销毁的事件,监听器类须要实现javax.servlet.ServletContextListener 接口。
ServletContextListener 是 ServletContext 的监听者,假设 ServletContext 发生变化,如server启动时 ServletContext 被创建,server关闭时 ServletContext 将要被销毁。
ServletContextListener接口的方法:
void contextInitialized(ServletContextEvent sce)
通知正在接受的对象,应用程序已经被载入及初始化。
void contextDestroyed(ServletContextEvent sce)
通知正在接受的对象,应用程序已经被载出。
ServletContextEvent中的方法:
ServletContext getServletContext()
取得ServletContext对象
2.ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包含:添加属性、删除属性、改动属性,监听器类须要实现javax.servlet.ServletContextAttributeListener接口。
ServletContextAttributeListener接口方法:
void attributeAdded(ServletContextAttributeEvent scab)
若有对象增加Application的范围,通知正在收听的对象
void attributeRemoved(ServletContextAttributeEvent scab)
若有对象从Application的范围移除,通知正在收听的对象
void attributeReplaced(ServletContextAttributeEvent scab)
若在Application的范围中,有对象代替还有一个对象时,通知正在收听的对象
ServletContextAttributeEvent中的方法:
java.lang.String getName()
回传属性的名称
java.lang.Object getValue()
回传属性的值
二、HttpSession相关监听接口
1.HttpSessionBindingListener接口
注意:HttpSessionBindingListener接口是唯一不须要再web.xml中设定的Listener
当我们的类实现了HttpSessionBindingListener接口后,仅仅要对象增加 Session范围(即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的 removeAttribute方法的时候或Session Time out的时候)时,容器分别会自己主动调用下列两个方法:
void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)
思考:怎样实现记录站点的客户登录日志, 统计在线人数?
2.HttpSessionAttributeListener接口
HttpSessionAttributeListener监听HttpSession中的属性的操作。
当在Session添加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被又一次设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比較类似。
3.HttpSessionListener接口
HttpSessionListener监听 HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
4.HttpSessionActivationListener接口
主要用于同一个Session转移至不同的JVM的情形。
四、ServletRequest监听接口
1.ServletRequestListener接口
和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
2.ServletRequestAttributeListener接口
和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
有的listener可用于统计站点在线人数及訪问量。 例如以下:
server启动时(实现ServletContextListener监听器contextInitialized方法),读取数据库,并将其用一个计数变量保存在application范围内
session创建时(实现HttpSessionListener监听器sessionCreated方法),读取计数变量加1并又一次保存
server关闭时(实现ServletContextListener监听器contextDestroyed方法),更新数据库
转自: http://www.blogjava.net/wx886104/archive/2010/06/01/322419.html
http://hht83.blog.163.com/blog/static/44037112008324232278/
web.xml在listener作用与用途的更多相关文章
- web.xml中listener作用及使用
一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...
- web.xml的contextConfigLocation作用及自动加载applicationContext.xml
web.xml的contextConfigLocation作用及自动加载applicationContext.xml 转自:http://blog.csdn.net/sapphire_aling/ar ...
- 谈谈对XML的理解?说明Web应用中Web.xml文件的作用?
谈谈对XML的理解?说明Web应用中Web.xml文件的作用? 解答:XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard ...
- web.xml中Listener的作用
Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应 ...
- web.xml文件的作用
每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗? 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. ...
- web.xml文件的作用及基本配置
Java的web工程中的web.xml文件有什么作用呢?它是每个web工程都必须的吗? 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. 那什么时候需要 ...
- 一个web项目中web.xml<context-param>的作用
转 <context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置 ...
- Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(优先初始化Spring IOC容器)
JavaWebSpringTomcatCache 最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口,并且 ...
- Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(例如顺序:1、初始化spring容器,2、初始化线程池,3、加载业务代码,将数据库中数据加载到内存中)
最近公司要做功能迁移,原来的后台使用的Netty,现在要迁移到在uap上,也就是说所有后台的代码不能通过netty写的加载顺序加载了. 问题就来了,怎样让迁移到tomcat的代码按照原来的加载顺序进行 ...
随机推荐
- [LeetCode] Longest Palindrome Substring 具体分析
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 关于 pthread_cond_wait 和 pthread_cond_signal , signal 无效的问题
关于一个消费者模式,,,引起的问题.. 我在io线程里不断的把一个函数调用放到队列里 然后ruby线程就不断的从这个队列里取出函数之争并运行. 典型的 消费者模式. 我曾经以为是这样... 这是wor ...
- Android: Receiving Data from the Send Intent,自己app注册系统分享
当用户在系统的专辑,点击共享时.通过我们自己的app.分享此图片. 1.注册 主要是在AndroidManifest.xml中,对activity注冊Intent-filter.如: <acti ...
- System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
原文 System.BadImageFormatException: 试图加载格式不正确的程序. (异常来自 HRESULT:0x8007000B) 用C#调用DLL文件,运行后报错如下: Syste ...
- HDU 4085 Steiner树模板称号
Dig The Wells Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- SAP RFC 函数来创建 Java呼叫 学习总结 一步一步的插图
前言 该公司很快就接到了一个项目,SAP有接口.让我们做老大SAP.首先SAP联系.但发展从来没有打过.本周集中在这一个研究. 各种碰壁,SAP该系统让我怎么说? 算了.说多了都是泪,以下附上本周学习 ...
- React学习系列
React学习系列 系列学习react 翻译地址 https://scotch.io/tutorials/learning-react-getting-started-and-concepts 我是初 ...
- MP4文件格式具体解释——结构概述
MP4文件格式具体解释(ISO-14496-12/14) Author:Pirate Leo Email:codeevoship@gmail.com 一.基本概念 1. 文件,由很多Box和FullB ...
- 一个非常不错的gridview 风格
<style type="text/css"> <!-- .datable {background-color: #9FD6FF; color:#333333; ...
- .NET 中易混淆的概念(Delegate vs Event)
事件(event)是一个非常重要的概念,我们的程序时刻都在触发和接收着各种事件:鼠标点击事件,键盘事件,以及处理操作系统的各种事件.所谓事件就是 由某个对象发出的消息.比如用户按下了某个按钮,某个文件 ...