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"