# 什么叫监听器
内容介绍
一、AWT
# 一、AWT
1:初次相见:AWT
范例:
import java.awt.FlowLayout;
import java. awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing. JButton;
import javax.swing. JFrame;
public class Demo1 {
public static void main (String [ ] args) {
JFrame frane = new JFrame ( );
frame.setSize ( 300,200) ;
frame.setLocation (200,200) ;
frame.setLayout (new FlowLayout ( ) );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton ("确定``");
frame.add(btn) ;
btn.addActionListener(new ActionListener () {
public void actionPerformed(Actiorvent evt) {
System.out.println ( "hello") ;
}
});
Frame.setVisible(ture);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2.二次相见:SAX
3、监听器:
l 它是一个接口,内容由我们来实现;
l 他需要注册,例如注册在按钮上;
监听器中的方法,会在特殊事件发生时被调用;
4、观察者:
l 事件源;
Ø 小偷
l 事件;
Ø 偷东西
l 监听器;
Ø 警察
Ø 监听器中的方法:抓捕
# 监听器概述以及生命周期监听器
内容简介:
1、 事件源
2、 javaWeb 中完成编写监听器
3、 事件对象
# 1、事件源:三大域
ServletContext
生死监听:ServletContextListener,它有两个方法,一个在出生时调用,一个在死亡时调用;
void contextInitialized(ServletContextEvent sce):创建 Servletcontext 时
void contextDestroyed(ServletContextEvent sce):销毁 Servletcontext 时
属性监听:ServletContextAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
HttpSession
生死监听:HttpSessionListener,它有两个方法,一个在出生时调用,一个在死亡时调用;
void sessionCreated( HttpSessionEvent se):创建 session 时
void sessionDestoryed( HttpSessionEvent se):销毁 session 时
属性监听:: HttpSessioniAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用;
ServleRequest
生死监听:ServletRequestListener,它有两个方法,一个在出生时调用,一个在死亡时调用;
void requestlnitialized(ServletRequestEvent sre):创建 request 时
void requestDestoryed (servletRequestEvent sre):销毁 request 时
属性监听:ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。
# 2、javaWeb 中完成编写监听器
写一个监听器类,要求必须去实现某个监听器接口;
注册,是在 web.xml 中配置来完成注册;
# 3、事件对象
SerxletContextExent:serxletContext getSerxletContext()
HttpSessionExent : HttpSession getSession()
SerMletReguest :
ServletContext getSerxletContext()
ServletRegues getSerxletReguest()
2
3
4
5
# 监听器之属性监听器
# 以 ServletContextAttributeListener 举例:
三个方法:
void attributeAdded(ServletContextAttributeEvent event)
Receives notification that an attribute has been added to the ServletContext.
void attributeRenoved(ServletcontextAttribut eEvent event)
Receives notification that an attribute has been removed from the ServletContext.
Void attributeReplaced(ServletContextAttributeEvent event)
ServletContextAttributeEvent 可以获取
getServletContext 信息,还可以得到添加属性的名称和值
写第二个监听器类:
第一种方法:
paakage cn.itcast.web-listener,
import javax.servlet.ServletContextAttributeEvent;
public class BListener implements
ServletContextAttributeListener{
public void attributeAdded (ServletContextAttributeEvent
scab){
system.out.println ( “您向 application 中添加了一个名为"+
scab.getName()+",值为:"+scab.getValue()+"的属性");
}
public void attributeReplaced(ServletContextAttributeEventacab){
}
public void attributeRemowed(ServletContextAttributeEvent acab){
}
}
添加到主页
<%
application.setAttribute("xxx","XXX");
%>
访问
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
第二种方法:
package cn.itcast.web.listener;
import javax.servlet.servletContextAttributeEvent;
public class BListener implements
servletContextAttributeListener{
public void attributeAdded (ServletcontextAttributeEvent scab)
{
System.out.println("您向 application 中添加了一个名为"+ scab.getName () + ",值为:"+scab.getvalue () +"的属性");
public void attributeReplaced(ServletcontextAttributeEvent scab) {System.out.println (scab.getName ( ) +"=" +
scab.getvValue() + ","+ scab.getServletContext ( )
.getAttribute (scab.getName ());
public void attributeRemoved(ServletContextAttributeEvent scab){
}
}
当<%
application.setAttribute ( "xxx","XXX");
%>
中的“XXX”改成“YYY”或“ZZZ”
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
第三种方法:
package cn.itcast.web.listener;
import javax.servlet.ServletContextAttributeEvent;
public glass BListener implements
ServletContextAttributeListener {
public void attributeAdded(servletcontextAttributeEvent scab){System.out.println("您向 application 中添加了一个名为" + scab.getName () + ",值为:"+scab-getValue ( +"的属性");}
public void attributeReplaced(ServletContextattributeEventscab){
System.out.println(scab.getName ()+"=" + scab.getValue()+","+
scab.getServletContext ().getAttribute(scab.getName () )) ;``}
public void attributeRemoved(ServletContextAttributeEvent acab){
system.out.println (acab.getName() + "=" + scab.getvalue ());}
}
<%@taglib prerix-" o" uri-" http:// java.
sun.com/jsp/jstl/oore" %>
My JSP 'remove-jsp' starting page
<%
application . removeAttribute ("xxx")
%>
访问
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18