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中配置来完成注册!
注意:是包名.类名
事件对象:汇报事件源的
>>ServletContextEvent:ServletContext getServletContext()
>> HttpSessionEvent:HttpSession getSession()
>>ServletRequest:
>>> ServletContextgetServletContext();
>>>ServletRequesgetServletRequest();
>>ServletContextAttributeEvent:
>>>ServletContextgetServletContext();
>>> StringgetName():获取属性名
>>> ObjectgetValue():获取属性值
>> HttpSessionBindingEvent:同上
>> ServletRequestAttributeEvent :同上
后两个是感知监听(都与HttpSession相关)
>>它用来添加到JavaBean上,而不是添加到三大域上!
>> 这两个监听器都不需要在web.xml中注册!
事件源:三大域
>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抛弃了我"); } }
相关文章推荐
- 使用Eclipse自带Web Service插件(Axis1.4)生成Web Service服务端/客户端
- 使用Eclipse自带的Axis1插件生成WSDL文件
- IDEA15使用maven编译scala和java
- Java中生成随机数
- IDEA15使用maven编译scala和java
- Java复习笔记(二)——环境变量Path和CLASSPATH的作用
- 快速搭建J2EE开发环境主要配置参数
- SpringMVC+Mybatis+Activiti5+Quartz动态配置+Solr4+Extjs5.1自主研发平台
- LeetCode : Longest Substring Without Repeating Characters [java]
- Java基本数据类型
- SpringIOC 实例化Bean的流程
- [Android]Eclipse连不上模拟器的问题[emulator-5554 disconnected]
- Spring MVC测试
- JDK7与JDK8的新特性
- Java遍历二维数组,求和
- eclipse反编译jar
- 快速排序的一个Java实现
- IT十八掌作业_java基础第三天_数组
- 跟着开涛学SpringMVC
- thinking in java 21_03