监听器

Listener-监听器

在javaweb中,listener监听的事件源分为ServletContecxt,HttpSessoin,ServletRequest三个级别

ServletContext级别
Listener场景
ServletContextListener响应ServletContext生命周期事件(创建/销毁在ServletContext创建或销毁时调用对应的方法)
ServletContextAttributeListener响应ServletContext属性的添加/删除/替换事件
HttpSession级别
Listener场景
HttpSessionListener响应HttpSession生命周期事件(创建/销毁)
HttpSessionAttributeListener响应HttpSession属性的添加/删除/替换事件
HttpSessionBindingListener实现该接口的Java Bean会在被Session添加/删除时作出响应
HttpSessionActivationListener实现该接口的JavaBean会在被Session钝化/激活时作出响应
Servletrequest级别
Listener场景
ServletrequestListener响应ServletRequest生命周期事件(创建/销毁)
ServletRequestAttributeListerner响应ServletRequest属性添加/删除/替换事件

注册

创建监听器只需要实现相应的接口,但我们还需要将其注册到Servlet容器当中,只有这样才能在事件发生时监听器作出响应。监听器的注册方式有注解和部署描述符两种

@WebListener

在servlet3.0当中提供了@WebListener注解
“`java
@WebListener
public class ListenerClass implements ServletContextListener {

  // ...

}
“`

部署描述符

<listener>
    <listener-class>com.fq.web.listener.ListenerClass</listener-class>
</listener>

因为HttpSessionBindingListener和HttpSessionActivationListener是直接绑定到JavaBean当中而非绑定到seesion等域对象中,因此可以不用注册

示例

HttpSessionListener
@WebListener
public class HttpSessionListenerImpl implements HttpSessionListener {


  @Override
  public void sessionCreated(HttpSessionEvent httpSessionEvent) {
      //监听HttpSesssion对象创建事件
      //获取被创建的session
      HttpSession session = httpSessionEvent.getSession();
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
      //监听HttpSesssion对象销毁事件
      //获取将要被销毁的Session
      HttpSession session  = httpSessionEvent.getSession();
  }
}

````





<div class="se-preview-section-delimiter"></div>

#### HttpSessionAttributeListener





<div class="se-preview-section-delimiter"></div>

```java
@WebListener
public class HttpSessionAttributeImpl implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(HttpSessionBindingEvent httpSessionBindingEvent) {
        //响应HttpSession对象添加事件
        //获取HttpSession对象
        HttpSession session = httpSessionBindingEvent.getSession();

        //添加的属性名称
        String name  = httpSessionBindingEvent.getName();

        //添加的属性值
        Object value = httpSessionBindingEvent.getValue();

        System.out.println("add attribute|"+name+":"+ value);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent httpSessionBindingEvent) {
        //响应HttpSession对象删除事件
        //获取HttpSession对象
        HttpSession session = httpSessionBindingEvent.getSession();

        //添加的属性名称
        String name = httpSessionBindingEvent.getName();

        //添加的属性值
        Object value = httpSessionBindingEvent.getValue();

        System.out.println("remove attribute|"+name+":"+ value);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent httpSessionBindingEvent) {
        //响应HttpSession对象替换事件
        //获取HttpSession对象
        HttpSession session = httpSessionBindingEvent.getSession();

        //添加的属性名称
        String name = httpSessionBindingEvent.getName();

        //添加的属性值
        Object value = httpSessionBindingEvent.getValue();

        System.out.println("replace attribute|"+name+":"+ value);
    }
}
@WebServlet(urlPatterns = "/session")
public class SessionServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doPost(req,resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      //get session
      HttpSession session = req.getSession();

      //添加属性
      session.setAttribute("msg","add attribute");

      //替换属性值
      session.setAttribute("msg","msg has already replaced");
      //删除属性msg
      session.removeAttribute("msg");
  }
}
HttpSessionBindingListener

public class Person implements Serializable,HttpSessionBindingListener {

  private int id;

  private String name;

  private double weight;


  public Person(int id,String name,double weight){
      this.id = id;
      this.name = name;
      this.weight = weight;
  }


  /**
   * 响应对象被添加到session事件
   */
  @Override
  public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
      System.out.println("我被添加到sesssion中了");
  }

  /**
   * 响应对象被session删除事件
   * @param httpSessionBindingEvent
   */
  @Override
  public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
      System.out.println("我被session删除了");
  }
}
@WebServlet(urlPatterns = "/sessionBinding")
public class SessionBindingServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      doPost(req,resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

      Person person = new Person(1,"luffy",50.1);

      HttpSession session = req.getSession();

      session.setAttribute("person",person);

      session.removeAttribute("person");
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值