1. 官方文档
文档:java_ee_api_中英文对照版.chm
2. Listener 监听器介绍
- Listener 监听器它是 JavaWeb 的三大组件之一。JavaWeb 的三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器
- Listener 是 JavaEE 的规范,就是接口
- 监听器的作用是,监听某种变化(一般就是对象创建/销毁, 属性变化), 触发对应方法完成相应的任务
- JavaWeb 中的监听器(共八个), 目前最常用的是 ServletContextListener, 后面案例演示.
3. JavaWeb 的监听器
3.1 ServletContextListener 监听器
-
作用:监听 ServletContext 创建或销毁(当我们 Web 应用启动时,就会创建 ServletContext),即生命周期监听
应用场景:
(1)加载初始化的配置文件;比如 spring 的配置文件
(2)任务调度(配合定时器 Timer/TimerTask) -
相关方法
-
应 用 实 例
HspServletContextListener.java
/**
* 解读
* 1. 当一个类实现了 ServletContextListener
* 2. 该类就是一个监听器
* 3. 该类可以监听的事件 由该类实现的监听接口决定,比如 实现ServletContextListener
* , 则该类就可以监听 ServletContext对象的创建和销毁, 以此类推
* 4. HspServletContextListener 就是一个监听者
* 5. 当web应用启动时,就会产生 ServletContextEvent 事件, 会调用监听器的对应事件处理方法
* contextInitialized, 同时会传递 事件对象
* 6. 程序员可以通过 ServletContextEvent 事件对象,来获取需要的信息, 然后再进行业务处理
* 7. tomcat怎么知道这个监听器存在 ? 因为我们需要在web.xml中配置
*/
public class HspServletContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
System.out.println("HspServletContextListener 监听到 " +
servletContext + " 被创建..");
//如果我们获取到ServletContext 对象..进行业务处理
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
System.out.println("HspServletContextListener 监听到 " +
servletContext + " 被销毁..");
//比如可以对servletContext 数据进行处理, 或者日志的管理...
System.out.println("进行处理工作.....");
}
}
web.xml
<!--配置监听器-->
<listener>
<listener-class>com.hspedu.listener.HspServletContextListener</listener-class>
</listener>
- 完成测试
3.2 ServletContextAttributeListener 监听器
- 作用:监听 ServletContext 属性变化
- 相关方法
- 使用少,再给大家举个例(后面的监听器类似)
HspServletContextAttributeListener.java
public class HspServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("HspServletContextAttributeListener 监听到添加属性.."
+ servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
}
@Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("HspServletContextAttributeListener 监听到删除属性.."
+ servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
}
@Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
System.out.println("HspServletContextAttributeListener 监听到修改属性.."
+ servletContextAttributeEvent.getName() + "=" + servletContextAttributeEvent.getValue() );
}
}
hi.java
public class HiServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//给servletContext 对象操作属性
ServletContext servletContext = request.getServletContext();
servletContext.setAttribute("name", "凤凰龙神号");
servletContext.setAttribute("name", "剑王龙神号");
servletContext.removeAttribute("name");
System.out.println("HiServlet 处理完毕....");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
web.xml
<listener>
<listener-class>com.hspedu.listener.HspServletContextAttributeListener</listener-class>
</listener>
<servlet>
<servlet-name>HiServlet</servlet-name>
<servlet-class>com.hspedu.servlet.HiServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HiServlet</servlet-name>
<url-pattern>/hi</url-pattern>
</servlet-mapping>
- 完成测试
3.3 其它监听器-使用较少
3.3.1 HttpSessionListener 监听器
- 作用:监听 Session 创建或销毁,即生命周期监听
- 相关方法
HspHttpSessionListener.java
public class HspHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
//当session创建时,我们给它设置一个生命周期 30s
session.setMaxInactiveInterval(30);
System.out.println("HspHttpSessionListener 监听到 session创建= " +
session.getId());
System.out.println("用户id=" + session.getId() + " 上线");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
System.out.println("HspHttpSessionListener 监听到 session销毁= " +
session.getId());
System.out.println("用户id=" + session.getId() + " 离线");
}
}
web.xml
<listener>
<listener-class>com.hspedu.listener.HspHttpSessionListener</listener-class>
</listener>
public class HiServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
System.out.println("HiServlet 处理完毕....");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
- 使用方法和前面一样, 可以用于监控用户上线,离线
3.3.2 HttpSessionAttributeListener 监听器
- 作用:监听 Session 属性的变化
- 相关方法
public class HspHttpSessionAttributeListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
//HttpSession session = httpSessionBindingEvent.getSession();
System.out.println("HspHttpSessionAttributeListener 监听到session添加属性" +
httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue());
}
@Override
public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
HttpSession session = httpSessionBindingEvent.getSession();
System.out.println("HspHttpSessionAttributeListener 监听到session删除属性" +
httpSessionBindingEvent.getName());
}
@Override
public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
System.out.println("HspHttpSessionAttributeListener 监听到session修改属性" +
httpSessionBindingEvent.getName() + "=" + httpSessionBindingEvent.getValue());
}
}
public class HiServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
session.setAttribute("age", 100);
session.setAttribute("age", 400);
session.removeAttribute("age");
System.out.println("HiServlet 处理完毕....");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
web.xml
<listener>
<listener-class>com.hspedu.listener.HspHttpSessionAttributeListener</listener-class>
</listener>
- 使用少 , 使用方法和前面一样。
3.3.3 ServletRequestListener 监听器
- ServletRequestListener 监听器
- 作用:监听 Request 创建或销毁,即 Request 生命周期监听
相关方法
- 可以用来监控, 某个 IP 访问我们网站的频率, 日志记录 ,访问资源的情况.
public class HspRequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("HspRequestListener 监听到 request对象创建");
ServletRequest servletRequest = servletRequestEvent.getServletRequest();
System.out.println("记录访问日志....");
System.out.println("访问IP= " + servletRequest.getRemoteAddr());
System.out.println("访问的资源= " + ((HttpServletRequest)servletRequest).getRequestURL());
}
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("HspRequestListener 监听到 request对象被销毁");
}
}
web.xml
<listener>
<listener-class>com.hspedu.listener.HspRequestListener</listener-class>
</listener>
3.3.4 ServletRequestAttributeListener 监听器
- 作用:监听 Request 属性变化
- 相关方法
- 使用方法和前面类似