JSP details-Servlet filters and listeners

Source: Internet
Author: User
Tags custom name

JSP details-Servlet filters and listeners
Filters and listeners

Servlet filters are newly added from the Servlet2.3 specification and are enhanced in the Servlet2.4 specification. Listeners can listen to Web application startup and shutdown. Create an interface for the listener to implement the response and configure it.

Servlet filter 1. What is a filter?

Servlet filters are very similar to Servlet filters, but they can intercept client requests. The Servlet filter can change the content in the request to meet the actual development needs. For program developers, the filter is essentially a Web application component on the Web application server, used to intercept requests between the client and the target resource, these requests are filtered and then sent to the target resource.

If you cannot solve the problem by using a filter in a Web application, you can deploy multiple filters to process service requests multiple times. In this way, a filter chain is formed. When the Web Container processes the filter chain, requests are processed in the order of filters.

2. filter core objects

Put the filter object in javax. in the servlet package, its name is Filter, which is an interface. In addition to this interface, the Filter-related objects include FilterConfig objects and FilterChain objects. These two objects are also interfaces, is located in javax. the servlet package contains the configuration object of the filter and the transfer tool of the filter. In actual development, you only need to implement the Filter interface directly or indirectly. FilterChain and FilterConfig are used for filter-related operations.

Filter Interface

Each Filter must implement the Filter interface directly or indirectly. In the Filter interface, three methods are defined: init (), doFilter (), and destroy (). The corresponding methods are described as follows:

Method Declaration

Description

Public void init (FilterConfig filterConfig) throws ServlerException

Filter initialization. This method is called during filter initialization.

Public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException

Filter requests

Public void destroy ()

Destruction method to release resources

FilterConfig Interface

The FilterConfig interface is implemented by the Servlet container and is mainly used to obtain the configuration information in the filter. The method is described as follows:

Method Declaration

Description

Public String getFilterName ()

Used to obtain the name of a filter.

Public ServletContext getServletContext ()

Get Servlet context

Public String getInitParameter (String name)

Obtains the value of the initialization parameter of the filter.

Public Enumeration getInitParameterNames ()

Obtain all the initialization parameters of the filter.

FilterChain Interface

The FilterChain interface is also implemented by Servlet containers. There is only one method in this interface, and its method declaration is: public void doFilter (ServletRuquest request, ServletResponse response) throws IOException, ServletException

This method is used to pass the filtered request to the next filter. If the filter is already the last filter in the filter, the request is sent to the target resource.

Filter creation and Configuration

To create a Filter, you must implement the javax. servlet. Filter interface and three methods of the interface.

Example:

Package com. zgy. filter;

Import java. io. IOException;

Import javax. servlet. Filter;

Import javax. servlet. FilterChain;

Import javax. servlet. FilterConfig;

Import javax. servlet. ServletException;

Import javax. servlet. ServletRequest;

Import javax. servlet. ServletResponse;

Public class MyFilter implements Filter {

@ Override

Public void destroy (){

// TODO Auto-generated method stub

}

@ Override

Public void doFilter (ServletRequest arg0, ServletResponse arg1,

FilterChain arg2) throws IOException, ServletException {

// TODO Auto-generated method stub

}

@ Override

Public void init (FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}

When using a filter, you do not have to pass the request to the next filter. If the business logic is required, you can directly respond to the client after the filter is processed.

Create a filter object named MyServlet. Its web. xml configuration is as follows:

MyFilter

Com. zgy. filter. Myfilter

MyFilter

/MyFilter

A tag is used to declare a filter object. In this tag, two elements must be configured: The Filter Name and the complete filter class name. Used to define the name of the filter, Specifies the full Class Name of the filter.

A tag is used to create a filter ing. Its main function is to specify which URLs should be processed by the filter in a Web application. In Tag, you need to specify the filtered name and filtered URL ing, where, Used to define the name of the filter, Specifies the URL of the filter application.

PS: Label It can be a custom name, Label Is the name of the specified defined filter. Label One-to-one correspondence



Related Article

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.