Servlet中监听器技术

来源:互联网 发布:网络授权书制作免费 编辑:程序博客网 时间:2024/05/17 21:42

Servlet中监听器技术


      • Servlet中监听器技术
        • 概述
        • 监听器开发步骤
        • 对象创建销毁的监听器接口使用
          • ServletRequestListener监听器接口使用
          • HttpSessionListener监听器接口使用
          • ServletContextListener监听器接口使用
        • 对象属性变化的监听器接口使用
          • ServletRequestAttributeListener属性监听器的使用
          • HttpSessionAttributeListener属性监听器的使用
          • ServletContextAttributeListener属性监听器的使用
        • session的其它类型监听器
          • HttpSessionBindListener监听器的使用
          • HttpSessionActivationListener监听session序列化以及反序列化事件


概述


监听器主要是用来监听特定对象的创建或销毁以及属性的变化,是一个实现特定接口的普通的Java类。对象从使用者的角度来说,主要分为两种:自己创建自己用和别人创建我拿来用,如果自己是自己创建的对象自己用是不用监听的,但是别人创建的我要拿来用所以需要使用监听器监听。在Servlet中,哪些对象需要监听呢?分别有 request/session/servletContext三种,分别对应的是request监听器、session监听器、servletContext监听器。监听器的英文是listener,下面将讲不同的监听器接口。

监听器开发步骤


  • 写一个普通的Java类,实现相关接口
  • 在web.xml(该文件称为部署项目描述文件)中配置,告诉服务器我创建了监听器

对象创建/销毁的监听器接口使用


该种监听器要被称为生命周期监听器,主要是监听对象的创建、销毁的过程。

ServletRequestListener监听器接口使用

监听request对象的创建与销毁,其代码实例如下:

  • MyRequestListener.java,实现了request对象监听器接口
package com.jpzhutech.listener;import javax.servlet.ServletRequestEvent;import javax.servlet.ServletRequestListener;public class MyRequestListener implements ServletRequestListener{    //对象创建    @Override    public void requestInitialized(ServletRequestEvent sre) {        System.out.println("对象创建");    }    //对象销毁    @Override    public void requestDestroyed(ServletRequestEvent sre) {        System.out.println("对象销毁");    }}
  • web.xml,告诉服务器创建了一个request对象监听器
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Listener</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- 监听request对象创建、销毁 -->  <listener>      <listener-class>com.jpzhutech.listener.MyRequestListener</listener-class>  </listener></web-app>
  • listener_test.jsp,激发request对象的创建与销毁
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'listener_test.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    test!  </body></html>
HttpSessionListener监听器接口使用

监听session对象的创建与销毁,其代码实例如下:

  • MyHttpSessionListener.java
package com.jpzhutech.listener;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class MyHttpSessionListener implements HttpSessionListener {    //对象创建    @Override    public void sessionCreated(HttpSessionEvent se) {        System.out.println("session对象创建");    }    //对象销毁    @Override    public void sessionDestroyed(HttpSessionEvent se) {        System.out.println("session对象那个销毁");    }}
  • web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Listener</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- 监听session对象创建、销毁 -->  <listener>      <listener-class>com.jpzhutech.listener.MyHttpSessionListener</listener-class>  </listener></web-app>
  • listener_test.jsp,激发session对象的创建与销毁
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'listener_test.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    test!  </body></html>
ServletContextListener监听器接口使用

监听servletContext对象的创建与销毁,其代码实例如下:

  • MyServletContextListener.java
package com.jpzhutech.listener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class MyServletContextListener implements ServletContextListener{    //对象销毁    @Override    public void contextDestroyed(ServletContextEvent arg0) {        System.out.println("servletContext对象销毁");    }    //对象创建    @Override    public void contextInitialized(ServletContextEvent arg0) {        System.out.println("servletContext对象创建");    }}
  • web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Listener</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- 监听ServletContext对象创建、销毁,在启动服务器时就会出现该监听器对象的监听结果 -->  <listener>      <listener-class>com.jpzhutech.listener.MyServletContextListener</listener-class>  </listener></web-app>
  • listener_test.jsp,激发servletContextListener对象的创建与销毁
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'listener_test.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    test!  </body></html>

对象属性变化的监听器接口使用


监听request/session/servletContext对象属性的变化。

ServletRequestAttributeListener属性监听器的使用

监听request对象属性变化的事件,比如属性被添加、移除、修改等。

HttpSessionAttributeListener属性监听器的使用

监听sessioin对象属性变化的事件,比如属性被添加、移除、修改等。

  • MySessionAttributeListener.java
package com.jpzhutech.attributelistener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;public class MySessionAttributeListener implements HttpSessionAttributeListener{    //属性的增加    @Override    public void attributeAdded(HttpSessionBindingEvent se) {        Object attribute = se.getSession().getAttribute("userName");        System.out.println("添加的属性是:"+attribute);    }    //属性的删除    @Override    public void attributeRemoved(HttpSessionBindingEvent se) {        System.out.println("session中属性移除");    }    //属性替换    @Override    public void attributeReplaced(HttpSessionBindingEvent se) {        //获取替换前的值以及获取被替换之后的属性值都可以    }}
  • web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Listener</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- HttpSessionAttributeListener属性监听器 -->  <listener>      <listener-class>com.jpzhutech.attributelistener.MySessionAttributeListener</listener-class>  </listener></web-app>
  • listener_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'listener_test.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    test!    <%       session.setAttribute("userName", "Jack");       session.removeAttribute("userName");     %>  </body></html>
ServletContextAttributeListener属性监听器的使用

监听servletContext对象属性变化的事件,比如属性被添加、移除、修改等。同上不再讲解代码的写法。

session的其它类型监听器


该类中的监听器了解即可。

HttpSessionBindListener监听器的使用

监听对象绑定/解除绑定到session上的事件,什么叫做对象绑定session?也就是将对象加入到session中,这样的话需要对象实现该类的接口,具体的开发步骤如下:

  • 对象实现HttpSessionBindListener
  • 再把对象绑定/解除绑定到session就会触发该监听器的代码

  • Admin.java

package com.jpzhutech.sessionbind;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;public class Admin implements HttpSessionBindingListener{    private int id;    private String name;    public Admin(int id, String name) {        super();        this.id = id;        this.name = name;    }    public Admin() {        super();    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    //对象放入到session中    @Override    public void valueBound(HttpSessionBindingEvent event) {        System.out.println("Admin对象放入到session中");    }    //对象从session中移除    @Override    public void valueUnbound(HttpSessionBindingEvent event) {        System.out.println("Admin对象从session中移除");    }}
  • listener_test.jsp
<%@page import="com.jpzhutech.sessionbind.Admin"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'listener_test.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    test!    <%       session.setAttribute("userInfo", new Admin());  //将Admin放入到session中,也就是所谓的绑定session     %>  </body></html>

注意:该监听器并没有在web.xml文件中进行配置,一定要注意。

HttpSessionActivationListener监听session序列化以及反序列化事件

略。

0 0
原创粉丝点击