java自定义监听器的简单介绍
本篇文章给大家谈谈java自定义监听器,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java 自定义事件的触发及监听
JAVA事件响应机制
1,先自定义一个事件
public class MyEvent extends java.util.EventObject{
public MyEvent(Object source)
{
super(source);
}
}
2,再自定义一个监听器
public class MyListener implements java.util.EventListener{
//这里是当事件发生后的响应过程
public void EventActivated(MyEvent me)
{
System.out.println("事件已经被触发");
}
}
3,以下这个类为触发事件的事件源
public class MyObject {
private Vector vectorListeners=new Vector();
public synchronized void addMyListener(MyListener ml)
{
vectorListeners.addElement(ml);
}
public synchronized void removeMyListener(MyListener ml)
{
vectorListeners.removeElement(ml);
}
protected void activateMyEvent()
{
Vector tempVector=null;
MyEvent e=new MyEvent(this);
synchronized(this)
{
tempVector=(Vector)vectorListeners.clone();
for(int i=0;itempVector.size();i++)
{
MyListener ml=(MyListener)tempVector.elementAt(i);
ml.EventActivated(e);
}
}
}
//定义一个公用方法用于触发事件
public void test()
{
activateMyEvent();
}
}
4,测试类
public class Test {
public static void main(String[] args)
{
MyObject mo=new MyObject();
//注册该事件
mo.addMyListener(new MyListener());
//触发该事件
mo.test();
}
}
java怎样监听一个值是否发生了变化,具体代码
java 自定义监听器监听属性变化
import java.util.EventObject;
public class MyEvent extends EventObject
{
private Object obj;
private String sName;
public MyEvent(Object source,String sName)
{
super(source);
this.obj=source;
this.sName=sName;
}
public Object getObj()
{
return obj;
}
public String getsName()
{
return sName;
}
}
import java.util.EventListener;
public interface MyEventListener extends EventListener
{
public void handleEvent (MyEvent me);
}
import java.util.Iterator;
import java.util.Vector;
import demo.DemoEvent;
public class MyEventSource
{
private Vector list=new Vector();
private String sName = "";
public MyEventSource()
{
super();
}
public void addMyEventListener(MyEventListener me)
{
list.add(me);
}
public void deleteMyEventListener(MyEventListener me)
{
list.remove(me);
}
public void notifyMyEvent(MyEvent me)
{
Iterator it=list.iterator();
while(it.hasNext())
{
((MyEventListener) it.next()).handleEvent(me);
}
}
public void setName(String str)
{
boolean bool = false;
if (str == null sName != null)
bool = true;
else if (str != null sName == null)
bool = true;
else if (!sName.equals(str))
bool = true;
this.sName = str;
// 如果改变则执行事件
if (bool)
notifyMyEvent(new MyEvent(this, sName));
}
public String getsName()
{
return sName;
}
}
public class Test implements MyEventListener
{
public Test()
{
MyEventSource mes = new MyEventSource();
mes.addMyEventListener(this);
mes.setName("niu");
}
public static void main(String args[])
{
new Test();
}
public void handleEvent(MyEvent me)
{
System.out.println(me.getSource());
System.out.println(me.getsName());
}
}
java事件监听器传参
可以采用内部类的方式,这样就不用考虑传参的问题,可以直接访问外部类的成员变量,如果一定要传参,那么就自定义一个监听器
class MyListener implements ActionListener{
private String str;
public MyListener(String str){
this.str = str;
}
}
如何用java实现自动监听处理jsp页面的
一、监听域对象中属性的变更的监听器
域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信息事件的监听器。
这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。
1.1、attributeAdded 方法
当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象
各个域属性监听器中的完整语法定义为:
public void attributeAdded(ServletContextAttributeEvent scae)
public void attributeReplaced(HttpSessionBindingEvent hsbe)
public void attributeRmoved(ServletRequestAttributeEvent srae)
1.2、attributeRemoved 方法
当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应
各个域属性监听器中的完整语法定义为:
public void attributeRemoved(ServletContextAttributeEvent scae)
public void attributeRemoved (HttpSessionBindingEvent hsbe)
public void attributeRemoved (ServletRequestAttributeEvent srae)
1.3、attributeReplaced 方法
当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应
各个域属性监听器中的完整语法定义为:
public void attributeReplaced(ServletContextAttributeEvent scae)
public void attributeReplaced (HttpSessionBindingEvent hsbe)
public void attributeReplaced (ServletRequestAttributeEvent srae)
1.4、ServletContextAttributeListener监听器范例:
编写ServletContextAttributeListener监听器监听ServletContext域对象的属性值变化情况,代码如下:
package me.gacl.web.listener;
import java.text.MessageFormat;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
/**
* @ClassName: MyServletContextAttributeListener
* @Description: ServletContext域对象中属性的变更的事件监听器
* @author: 孤傲苍狼
* @date: 2014-9-11 下午10:53:04
*
*/
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中添加了属性:{0},属性值是:{1}"
,scab.getName()
,scab.getValue());
System.out.println(str);
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中删除属性:{0},属性值是:{1}"
,scab.getName()
,scab.getValue());
System.out.println(str);
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中替换了属性:{0}的值"
,scab.getName());
System.out.println(str);
}
}
在web.xml文件中注册监听器
listener
descriptionMyServletContextAttributeListener监听器/description
listener-classme.gacl.web.listener.MyServletContextAttributeListener/listener-class
/listener
编写ServletContextAttributeListenerTest.jsp测试页面
%@ page language="java" pageEncoding="UTF-8"%
!DOCTYPE HTML
html
head
titleServletContextAttributeListener监听器测试/title
/head
body
%
//往application域对象中添加属性
application.setAttribute("name", "孤傲苍狼");
//替换application域对象中name属性的值
application.setAttribute("name", "gacl");
//移除application域对象中name属性
application.removeAttribute("name");
%
/body
/html
运行结果如下:
从运行结果中可以看到,ServletContextListener监听器成功监听到了ServletContext域对象(application)中的属性值的变化情况。
java自定义监听器的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java自定义监听器的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。