Introduction to servlet listener

Source: Internet
Author: User

When a web application runs in a Web Container, various internal events occur in the Web application, such as starting a web application, stopping a web application, starting a user session, and ending a user session. These web operations are usually transparent to developers. However, Servlet APIS also provide corresponding interfaces for calling.

To use listener, we only need two steps:

① Define the listener implementation class (implement the corresponding interface)

② Configure listener through annotation or in the web. xml file

1. Implement the listener class

Different web events correspond to different listeners. Common Web event listener interfaces include the following:

-> Servletcontextlistener: used to listen to the startup and shutdown of Web applications.

-> Servletcontextattributelistener: Used to listen for changes to attributes in the servletcontext range (application ).

-> Servletrequestlistener: used to listen to user requests

-> Servletrequestattributelistener: Used to listen for changes to attributes in the servletrequest range (request ).

-> Httpsessionlistener: used to listen to the start and end of a user session.

-> Httpsessionattributelistener: Used to listen for changes to attributes in the httpsession range (session ).

Generally, you only need to monitor the web time to implement the corresponding interface method.

2. Configure listener

After the listener class is implemented, you also need to configure listener. You can select annotaion or web. xml.

Annotation Method: You only need to use @ weblistener to modify the listener implementation class (more than servlet3.0 is required)

Web. xmlConfiguration:

<Listener> <! -- Specify the listener implementation class --> <listener-class> Lee. getconnlistener </listener-class> </listener>

The following is an example of starting a web application:

First, implement the listener class (implement the servletcontextlistener Interface ):

Package COM. chanshuyi. listener; import javax. servlet. servletcontextevent; import javax. servlet. servletcontextlistener; public class startuplistener implements servletcontextlistener {/*** close operation */Public void contextdestroyed (servletcontextevent SC) {system. out. println ("------------------ disable tomcat! ------------------ ");}/*** Start initialization */Public void contextinitialized (servletcontextevent SC) {// obtain the application. Unable to get request/response/session // servletcontext application = SC. getservletcontext (); system. Out. println ("------------------ start Tomcat! ------------------");}}

Then, configure in Web. xml:

<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> <listener> <! -- Specify the listener implementation class --> <listener-class> com. chanshuyi. listener. startuplistener </listener-class> </listener> </Web-app>

MARK: An error occurred while using the @ weblistener annotation. I don't know why. (Mark chenyr)

To listen to other web application events, the Implemented interfaces are different. The rest are the same.

Introduction to servlet 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.