Filter,listener (EXT)

Source: Internet
Author: User
Tags blank page

First, the function of filter
The filter function allows the user to change a request and modify a response. Filter is not a servlet, it cannot produce a response, it can preprocess the request before a request arrives at the servlet, or it can handle response when it leaves the servlet. In other words, filter is actually a "Servlet Chaining" (servlet chain).

A filter includes:

1), intercepted before the servlet is invoked;

2), check the servlet request before the servlet is invoked;

3), modify the request header and request data as required;

4), modify response head and response data as needed;

5), intercepted after the servlet is invoked.

The server only calls the Setfilterconfig method one time to prepare filter processing, and calls the Dofilter method multiple times to handle different requests. The Filterconfig interface has methods to find the filter name and initialization parameter information. Server can set Filterconfig NULL to indicate that the filter has been terminated.

Each filter gets the current request and response from the Dofilter () method. In this method, you can do any action for request and response. (including collecting data, packing data, etc.). Filter calls the Chain.dofilter () method to give control to the next filter. A filter in DoFilter () Method ends. If a filter wants to stop the request processing and get full control of the response, it can not call the next filter

1.Filter is a filter, filter some unfriendly or you do not want the east,
2.Filter can be used to filter and redirect requests, which means you can manipulate objects such as request and response,session.
3.Filter is the core controller of struts and is responsible for intercepting all user requests.
4.Filter (Filter): You can write a class to implement the filter interface, and then configure to Web. XML, then Tomcat will first call the first filter in Filterchain after receiving the HTTP request, in order to be able to invoke the next filter or the real request ( Servlet or JSP), so you must call Chain.dofilter (Request,response) in the method implemented by your implementation class, or you will get a blank page! Filters are often used for handling garbled, privilege control, and can also manage session! in hibernate
5.Filter is basically similar to a servlet that can be used as a servlet (struts2.0 is doing this) with Reqeust requests to run

Second, listener function

It is designed based on the observer pattern, and the Listener design provides a quick way to develop a Servlet application that facilitates the control of the program and data from another longitudinal dimension. Currently, there are 5 observer interfaces for two types of events in the Servlet: 4 eventlisteners types, Servletcontextattributelistener, Servletrequestattributelistener, Servletrequestlistener, Httpsessionattributelistener, and 2 LifecycleListeners types, Servletcontextlistener, Httpsessionlistener. As shown in the following:

Listener is a servlet listener that listens to client requests, service-side operations, and so on. Through the listener, you can automatically trigger some actions, such as the number of users listening online. When a httpsession is added, the sessioncreated (httpsessionevent se) method is fired, which adds 1 to the number of people online. There are several common listening interfaces:

Servletcontextattributelistener listens to operations on the ServletContext property, such as adding, deleting, and modifying properties.

Servletcontextlistener Monitor ServletContext. The contextinitialized (Servletcontextevent SCE) method is fired when the ServletContext is created, and when ServletContext is destroyed, the contextdestroyed is fired ( Servletcontextevent sce) method.

Httpsessionlistener Monitor the operation of the HttpSession. The session Created (Httpsessionevent se) method is fired when a session is created, and the sessiondestroyed (httpsessionevent se) method is fired when a session is destroyed.

Httpsessionattributelistener listens for properties in the httpsession operation. When an attribute is added to the session, the attributeadded (httpsessionbindingevent se) method is fired; When a property is deleted at the session, the attributeremoved is fired ( Httpsessionbindingevent se) method, which fires the attributereplaced (httpsessionbindingevent se) method when the session property is reset.

Below we develop a concrete example of how the listener can count the number of people online. When ServletContext is initialized and destroyed, the corresponding information is printed on the server console. When the attributes in ServletContext are added, changed, deleted, the corresponding information is printed on the server console.

To get the above functionality, the listener must implement the following 3 interfaces:

Httpsessionlistener

Servletcontextlistener

Servletcontextattributelistener


1.Listener is a servlet listener that listens to client requests, service-side operations, and so on.
With the listener, some actions can be automatically fired.
2.listner can only hear modifications to the properties of the above objects.
3.listener is a listener, usually listening and loading some plug-ins, such as spring. LOG4J, etc.
4.Listener (Listener): Using the Observer pattern implementation, in Tomcat, if you want to listen to objects such as application,session,request,response, to implement a similar ****listener interface, Re-configured into Web. XML, Tomcat will instantiate the listener classes you write as objects into a collection after booting, and then you will invoke the methods in the listener for the objects you want to listen to, such as Httpsessionlistener Can be used to listen to all the users currently online!
5.listener is the listener is the system starts on the operation of general monitoring is used to pre-load system files

(Transferred from http://blog.csdn.net/agileclipse/article/details/9014683 http://bbs.csdn.net/topics/390539970)

Web. XML Configuration <!--Listener & Filter Test--><!--Listener need to implement Javax.servlet.ServletContextListener interface-- <listener><listener-class>com.listenerdemo.MyListener1</listener-class></listener>< !--filter needs to implement Javax.servlet.Filter interface--><filter>              <filter-name>LogFilter</filter-name>              <filter-class>com.ee.filter.LogFilter</filter-class>          </filter>            < filter-mapping>              <filter-name>LogFilter</filter-name>              <url-pattern>/*</ Url-pattern>          </filter-mapping>                      

  

Filter,listener (EXT)

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.