Append a Filter to the processing pipeline of the request.

Source: Internet
Author: User

Append a Filter to the processing pipeline of the request.

Everyone should know that in ASP. net mvc, the C # annotation attribute is used to append a Filter to the Controller or action method.

Today, let's talk about another field method: automatically attaching the Filter we define to the Controller or action method, you do not need to manually use the C # annotation attribute feature on the Action method or controller.

Step 1

Define an interface IMonoFilterProvider. The interface name can be obtained without any methods or attributes. It serves as a tag interface. For example:

    public interface IMonoFilterProvider    {    }

Step 2

Defines a class that implements the System. Web. Mvc. IFilterProvider interface to obtain or set the object's filter information.

For example, in my Mono Project, a class is defined as follows:

    public class MonoFilterProvider : IFilterProvider    {        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)        {            var lifetimeScope = GetLifetimeScope();            var filterProviders =lifetimescope.Resolve<IEnumerable<IMonoFilterProvider>>();            return filterProviders.Select(x => new Filter(x, FilterScope.Action, null));        }    }

In the GetFilters () method, I use the IoC container to obtain the strength of all classes that implement the interfaces in the first step. If you use the IoC container, you must register all the classes that implement IMonoFilterProvider to the IoC container. That is to say, you can wait until all the instances that implement the classes in some way.

 

Step 3

Add the filter search class defined in step 2 to the global filter search class list of Mvc.

 FilterProviders.Providers.Add(new MonoFilterProvider());

This action can be performed either in the Application_Start () method or earlier.

 

Step 4

Define a filter. The method is the same as that provided by Mvc. You only need to add an implementation interface IMonoFilterProvider,

 

 


How do I configure the Filter content specified in the Filter?

Your problem seems to be that you only need Filter to process jsp requests. You only need to modify the Filter configuration in web. xml. Url-pattern: Do not give/* To/. jsp. If you need to filter multiple file requests, you can add filter-mapping. Each url-pattern corresponds to a file.
<Filter-mapping>
<Filter-name> </filter-name>
<Url-pattern> </url-pattern>
</Filter-mapping>
 
What should I write in the three methods of jsp filter?

1. Servlet filter Basics
Servlet filters are a special usage of Servlet and are mainly used to perform some common operations. For example, encoding filtering and user login status judgment. Applicable scenarios of Servlet filters:
A. Authentication Filtering
B. login and Audit filtering
C. Image conversion Filtering
D. Data Compression and filtering
E. Encrypted Filtering
F. Token Filtering
G. Resource Access trigger Event Filtering
Servlet filter interface composition:
All Servlet Filter classes must implement the javax. servlet. Filter interface. This interface contains three methods that must be implemented by the filter class:
Method description
Init (FilterConfig cfg) is the initialization method of the Servlet filter, which is equivalent to the init method of the servlet.
DoFilter (ServletRequest, ServletResponse, FilterChain) completes the actual filtering operation. When the request accesses the URL associated with the filter, the Servlet container will first call the doFilter method of the filter. FilterChain parameters are used to access subsequent filters.
The destroy () Servlet container calls this method before destroying the filter instance. In this method, resources occupied by the Servlet filter can be released ., It is equivalent to the destory () method of servlet.
To create a Servlet filter, follow these steps:
A. servlet class that implements the javax. servlet. Filter Interface
B. Implement the init method and read the initialization function of the filter.
C. Implement the doFilter method to respond to requests or filters
D. Call the doFilter method of the FilterChain interface object to pass requests or responses to subsequent filters.
F. Configure Filter in web. xml
2. Use a filter to solve Chinese problems
When using the user login page to enter the account, if the input is Chinese, the background servlet output this content again, may be garbled, this is because serlvet is encoded in ISO-8859-1 format by default, if there are multiple servlets and multiple parameters in the background, this is not suitable. We can solve this problem by using a single filter so that the output in the background supports Chinese! Code that transcodes the ISO-8859-1 to GBK!
3. Use a filter to authenticate users:
You can also configure initialization parameters for each Filter. You can configure addresses that do not need to be filtered to the configuration parameters of this Filter. When filtering, if the request address is in the configuration parameters, the request is allowed, this avoids hard coding in the program. During initialization of each Filter, you can obtain the configuration object. In the Filter, configure two addresses that do not need to be filtered. One is the login page and the other is the servlet that implements login authentication;
==== Gorgeous split line ====
Above is theoretical knowledge
The following is a practical example
The three methods in the Filter are actually only the doFilter method, and the other two are directly implemented by the parent class.
The following is an example of doFilter method overwrite:

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

// Code to be executed before the servlet processes the Request Response
Chain. doFilter (request, response );
// Execute the code to be done after the servlet processes the Request Response

}... Remaining full text>

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.