码迷,mamicode.com
首页 > 编程语言 > 详细

【Java EE 学习第21天】【其它类型的监听器】

时间:2015-06-22 09:52:28      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

一、ServletContextListener 

Method Summary

 void

contextDestroyed(ServletContextEvent sce)
          Receives notification that the ServletContext is about to be shut down.

 void

contextInitialized(ServletContextEvent sce)
          Receives notification that the web application initialization process is starting.

代码举例:

技术分享
package com.kdyzm.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext sc=sce.getServletContext();
        System.out.println(sc+"被销毁!");
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println(sce.getServletContext()+"初始化!");
    }
    
}
ServletContextListener举例

二、ServletContextAttributeListener

Method Summary

 void

attributeAdded(ServletContextAttributeEvent event)
          Receives notification that an attribute has been added to the ServletContext.

 void

attributeRemoved(ServletContextAttributeEvent event)
          Receives notification that an attribute has been removed from the ServletContext.

 void

attributeReplaced(ServletContextAttributeEvent event)

 使用方法和HttpSessionAttriuteListener的用法相似,略。

三、HttpSessionBindingListener

  1.功能:监听一个Bean是否被放到了Session中。

  2.特点:该接口需要被Bean实现才能正常发挥作用,实现该接口的Bean不需要配置到web.xml文件中。

  3.举例说明:

技术分享
package com.kdyzm.domain;

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

public class Person implements HttpSessionBindingListener{
    private String name;
    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + "]";
    }

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        System.out.println(this+"被加入到session中!");
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        System.out.println(this+"被移出session!");
    }
    
}
实现了HttpSessionBindingListener接口的Bean
技术分享
<%@page import="com.kdyzm.domain.Person"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" 
    contentType="text/html; charset=utf-8" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>Insert title here!</title>
  </head>
  
  <body>
    <%
        Person p=new Person("小黄");
        session.setAttribute("p", p);
        out.println(session.getAttribute("p"));
        session.removeAttribute("p");
        out.println(session.getAttribute("p"));
    %>
  </body>
</html>
index.jsp测试JSP文件

 

【Java EE 学习第21天】【其它类型的监听器】

标签:

原文地址:http://www.cnblogs.com/kuangdaoyizhimei/p/4592900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!