"Java EE Learning Day 21st" "Other types of listeners"

Source: Internet
Author: User

First, Servletcontextlistener

Method Summary

 void

contextDestroyed(ServletContextEvent sce)
Receives notification that the ServletContext was about to being shut down.

 void

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

code example:

 PackageCom.kdyzm.listener;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener; Public classMyservletcontextlistenerImplementsservletcontextlistener{@Override Public voidcontextdestroyed (Servletcontextevent SCE) {ServletContext sc=Sce.getservletcontext (); System.out.println (SC+ "Be destroyed!" "); } @Override Public voidcontextinitialized (Servletcontextevent sce) {System.out.println (Sce.getservletcontext ()+ "Initialize!" "); }    }
Servletcontextlistener Example

Second, Servletcontextattributelistener

Method Summary

 void

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

 void

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

 void

attributeReplaced(ServletContextAttributeEvent event)

The usage method is similar to that of Httpsessionattriutelistener, slightly.

Third, Httpsessionbindinglistener

1. Function: Listen to whether a bean is placed in the session.

2. Features: The interface needs to be implemented by the bean to function properly, the bean that implements the interface does not need to be configured in the Web. xml file.

3. For example:

 PackageCom.kdyzm.domain;Importjavax.servlet.http.HttpSessionBindingEvent;ImportJavax.servlet.http.HttpSessionBindingListener; Public classPersonImplementshttpsessionbindinglistener{PrivateString name;  PublicPerson () {} PublicPerson (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"Person [name=" + name + "]"; } @Override Public voidValuebound (Httpsessionbindingevent event) {System.out.println ( This+ "is added to the session! "); } @Override Public voidValueunbound (Httpsessionbindingevent event) {System.out.println ( This+ "was removed from session! "); }    }
a bean that implements the Httpsessionbindinglistener interface
<% @pageImport= "Com.kdyzm.domain.Person"%><%@ Pagelanguage= "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>    <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8">    <title>Insert title here!</title>  </Head>    <Body>    <% PersonP=newPerson ("Xiao Huang");        Session.setattribute ("P", p);        Out.println (Session.getattribute ("P"));        Session.removeattribute ("P");    Out.println (Session.getattribute ("P")); %>  </Body></HTML>
index.jsp testing JSP files

"Java EE Learning Day 21st" "Other types of listeners"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.