Filter filter, and Listener Listener

Filter is a component of running a web project, when the request came, the filter will be running inside the code, and then distributed to the resource request to the real request.
Filter Filters
Filter: a filter to filter requests or a plurality of Servlet, request to go through the filter, and then through the Servlet
response sent from the Servlet, and then through the filter will result in response to the browser.

The role of the filter:
the filter defining a plurality of common Servlet code without redundancy codes.

Filter development steps:

  1. Encoding the Filter the implements
    Here Insert Picture Description
    2. configure the filter of web.xml.
    Here Insert Picture Description
    URL-pattern filter disposed path, / * all are filtered.
    3. testing. How the servlet request before, and now how request, the request will automatically run the filter inside the code.

Note the use of filter:

1. The filter may filter any resources. Resources include servlet, html, images, css, jsp, js.
2. Configure web.xml time
/Filter all requests
/ LoginAction filter is LoignAction resource request path
/ person / filter included in the request path / person requests.
3. A filter-mapping the plurality of labels which can be configured url-pattern, url-pattern corresponds to a plurality of execution of the filter.
4. When there are a plurality of filters project, then the execution order of the plurality of filters do with the configuration sequence.

Filter Typical Applications:

1. Set the character set of codec
Here Insert Picture Description
Here Insert Picture Description
2. Forced Log
Here Insert Picture Description
Here Insert Picture Description

Listener (listener)

Listener: Object is responsible for monitoring special events, after listening to a particular event happens, a piece of Java code.
ServletContextListener: listening ServletContext object creation and destruction
start listening and unloading site
HttpSessionListener: listening session creation and destruction of objects
Monitor
Here Insert Picture Description

The life cycle:

Lifecycle Filter objects: is created when the web application starts
calling in the filter requests
destroyed when the web site is down
Servlet object life cycle: creation during a visit to
a special case:
Here Insert Picture Description
in an interview with browser access
destroyed when the web site is down

Guess you like

Origin blog.csdn.net/MacWx/article/details/92413575