您的位置:首页 > 编程语言 > Java开发

Javaweb中的监听器

2016-03-05 11:14 441 查看
Javaweb中的监听器

事件源:三大域

>ServletContext

>HttpSession

>ServletRequest

每个事件源分别对应两个监听器:

ServletContext

   >>生命周期监听,有两个方法,一个在出生时调用,另一个在死亡时调用,在服务器启动时出生,服务器关闭时死亡

     >>>public void contextInitialized(ServletContextEvent sce)

     >>>public void contextDestroyed(ServletContextEvent sce)

   >>属性监听

     >>>void attributeAdded(ServletContextAttributeEvent scab):添加属性时;

     >>>void attributeReplaced(ServletContextAttributeEvent scab):替换属性时

     >>>void attributeRemoved(ServletContextAttributeEvent scab):移除属性时

HttpSession

   >>生命周期监听,有两个方法,一个在出生时调用,另一个在死亡时调用

     >>>void sessionCreated(HttpSessionEvent se)

     >>>void sessionDestroyed(HttpSessionEvent se)

   >>属性监听

     >>> void
attributeAdded(HttpSessionBindingEvent event):添加属性时;

     >>>void attributeReplaced(HttpSessionBindingEvent event):替换属性时

     >>>void attributeRemoved(HttpSessionBindingEvent event):移除属性时

ServletRequest

   >>生命周期监听,有两个方法,一个在出生时调用,另一个在死亡时调用

     >>>void requestInitialized(ServletRequestEvent sre)

     >>>void requestDestroyed(ServletRequestEvent sre)

   >>属性监听

      >>>void attributeAdded(ServletRequestAttributeEvent srae):添加属性时

     >>> void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时

     >>> void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时

 javaWeb中完成编写监听器:

>写一个监听器类:要求必须去实现某个监听器接口;

>注册,是在web.xml中配置来完成注册!

<listener>
<listener-class>listener.AListener</listener-class>
</listener>

注意:是包名.类名

事件对象:汇报事件源的

   >>ServletContextEvent:ServletContext getServletContext()

   >> HttpSessionEvent:HttpSession getSession()

   >>ServletRequest:

     >>> ServletContextgetServletContext();

     >>>ServletRequesgetServletRequest();

   >>ServletContextAttributeEvent:

     >>>ServletContextgetServletContext();

     >>> StringgetName():获取属性名

     >>>  ObjectgetValue():获取属性值

   >> HttpSessionBindingEvent:同上

   >> ServletRequestAttributeEvent :同上

后两个是感知监听(都与HttpSession相关)

>>它用来添加到JavaBean上,而不是添加到三大域上!

>> 这两个监听器都不需要在web.xml中注册!

package listener;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class User implements HttpSessionBindingListener {
private String username;
private String password;

public User() {
super();
// TODO Auto-generated constructor stub
}

public User(String username, String password) {
super();
this.username = username;
this.password = password;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public void valueBound(HttpSessionBindingEvent hsbe) {
System.out.println("session添加了我");
}

public void valueUnbound(HttpSessionBindingEvent ahsbe) {
System.out.println("session抛弃了我");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: