Javaweb Development--servlet Listener Listener

Source: Internet
Author: User
Tags event listener

First, the concept of servlet listener

A servlet listener is a special class defined in the servlet specification that listens to the creation and destruction events of domain objects such as ServletContext, HttpSession, and ServletRequest.

and listening for events in which the properties in these domain objects have been modified.

Listener object:

1, servletcontext:application, the entire application exists only one

2, httpsession:session, for each dialogue

3, Servletrequest:request, for each customer request

Listener content: Create, destroy, attribute change events

Monitoring role: Can be in the event before, after the occurrence of some processing, generally can be used to count the number of online and online users, statistics website visits, system startup initialization information.

Second, the basic use of the listener

To create a step:

1. Create a class that implements the Listener interface

2. Configure Web. xml file, Register Listener

<listener>    <listener-class> Full path name </listener-class></listener>

Boot order of the listener: start in the configuration order of Web. xml

Load Order: Listener > Filter >servlet

Iii. Classification of listeners

3.1 According to the listener's object division

1. Event listener for monitoring Application Environment object (ServletContext), implement Servletcontextlistener, Servletcontextattributelistener interface

2, for listening to the user session object (httpseesion) event listener, implementation of Httpsessionlistener, Httpsessionattributelistener interface

3, for listening to the request message object (ServletRequest) event Listener, implement Servletrequestlistener, Servletrequestattributelistener interface

3.2 According to the Listening events Division 

The Servletcontextlistener, Httpsessionlistener and Servletrequestlistener interfaces are implemented according to the different listener objects.

Project initialization information can be configured in Web. xml

<context-param>    <param-name> Property name </param-name>    <param-value> attribute value </param-value ></context-param>

Custom Listener

 Public classMyfirstlistener implements servletcontextlistener{ Public voidcontextinitialized (Servletcontextevent sce) {//get the properties configured in Web. XMLString Value=sce.getservletcontext (). Getinitparameter ("Property name"); System. out. println (value); }     Public voidcontextdestroyed (Servletcontextevent sce) {//Action on Shutdown}}

Web. XML config Session timeout parameter, unit: minutes, Session timeout time is not accurate

<session-config>    <session-timeout></session-timeout></session-config>

Javaweb Development--servlet Listener Listener

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.