How to get started with web. xml: A thorough listener and a web. xml listener

Source: Internet
Author: User

How to get started with web. xml: A thorough listener and a web. xml listener

Listener principle:

Monitored object → events owned by the object → event changes captured → events captured by the listener → events handled by the listener

The Web server has four scopes: The page range, the request range, the session range, and the application range. The listener can be used to monitor when these range objects are created, when they are destroyed, when data is stored in the range, and when the stored data is replaced.

Servlet listeners mainly include:

1. Listen to the servletContext domain object

2. Listen to HttpSession domain 3 and HttpRequest domain objects. There are eight listening interfaces.

Purpose:

  A listener is a common java program that implements a specific interface. This program is used to listen for method calls or attribute changes of another java object. After the above events occur on the Monitored object, A Method of the listener will be executed immediately.

Configuration:

  1. Let's take a look at the <listener> organizational structure. A simple answer: it only specifies a java class of the listener. Add a simple description of the listener and name it.

 

 

2. ServletContext listener. The custom listener must implement the ServletContextListener listener.

 

 

 

 

3. In order to test the method in which the listener closes the container, a fatal error is thrown, causing the container to crash immediately after it is started. In web. xml, the filter class name is deliberately not specified.


4. In spring, the org. springframework. web. context. ContextLoaderListener class listener also implements the ServletContextListener interface. Load the spring configuration file. Check the source code. To use the spring listener, you must import the corresponding jar package.

 

5.

Another common method is to configure log4j logs in spring. Logs are important when we write programs for debugging. Therefore, you must configure the logs. There are other advantages:

1. dynamically change the record level and policy without restarting the Web application.

2. Set the log file to any directory under/WEB-INF/, without the need to write the absolute path.

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.