Listener (listener)

Source: Internet
Author: User

Listener is a servlet listener that can listen to client requests and server operations.

Procedure for defining the listener class

1) To make a Class A listener class, you must implement the listener interface and interface implementation methods,

Common listening interfaces are as follows:

  • Servletcontextlistener
  • Servletcontextatrributelistener
  • Httpsessionlistener
  • Httpsessionattributelistener

2) configure the relevant information in Web. xml

For example

<1> define a listener class:

Package COM. servlet. listener; </P> <p> Import javax. servlet. servletcontextevent; <br/> Import javax. servlet. servletcontextlistener; </P> <p> // servletcontextlistener listener <br/> // The Listener initialization takes precedence over Servlet and filter, destruction is slower than Servlet and filter. <br/> public class myservletcontextlistener implements servletcontextlistener {</P> <p> // call this method when servletcontext is created during initialization. <br/> public void contextinitialized (servletcontextevent SCE) <br/>{< br/> system. out. println ("listener contextinitialized:" + SCE. getservletcontext (); <br/>}< br/> // call this method when servletcontext is destroyed <br/> Public void contextdestroyed (servletcontextevent SCE) <br/>{< br/> system. out. println ("listener contextdestroyed:" + SCE. getservletcontext (); <br/>}</P> <p >}< br/>

After defining a class, you need to configure the information in Web. xml before configuring the filter and Servlet configurations,

<2> the configuration information is as follows:

<Listener> </P> <p> <listener-class> com. servlet. listener. myservletcontextlistener </listener-class> </P> <p> </listener>

<1> define a listener class

Package COM. servlet. listener; </P> <p> Import javax. servlet. servletcontextattributeevent; <br/> Import javax. servlet. servletcontextattributelistener; </P> <p> public class myservletcontextattributelistener implements <br/> servletcontextattributelistener {</P> <p> // when an attribute value is added, call this method <br/> Public void attributeadded (servletcontextattributeevent scab) {<br/> // todo auto-generated method stub </P> <p> system. out. println ("attributeadded:"); </P> <p> system. out. println ("name:" + scab. getname () + "value:" + scab. getvalue (); </P> <p >}</P> <p> // when a property value is removed, call <br/> Public void attributeremoved (servletcontextattributeevent scab) {<br/> // todo auto-generated method stub <br/> system. out. println ("attributeremoved"); </P> <p> system. out. println ("name:" + scab. getname () + "value:" + scab. getvalue (); </P> <p >}</P> <p> // when an attribute value is replaced, call this method <br/> Public void attributereplaced (servletcontextattributeevent scab) {<br/> // todo auto-generated method stub </P> <p> system. out. println ("attributereplaced"); </P> <p> // scab. getvalue () returns the replaced value, which is the old value. <br/> system. out. println ("name:" + scab. getname () + "value:" + scab. getvalue (); </P> <p >}< br/>

<2> Web. xml configuration information:

<Listener> </P> <p> <listener-class> com. servlet. listener. myservletcontextattributelistener </listener-class> </P> <p> </listener>

The other listener classes are similar to the above two.

Listener initialization (the initialization method of servletcontextlistener) takes precedence over Servlet and filter, while destruction is slower than Servlet and filter.





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.