Interceptor and filter

Source: Internet
Author: User

Java code
Many people know about filters and have heard of interceptors. However, if you really cannot tell the differences between filters, You can briefly describe them:
1. the interceptor is based on the java reflection mechanism, and the filter is based on the function callback.
2. The filter depends on the servlet container, while the interceptor does not depend on the servlet container.
3. the interceptor can only work on action requests, while the filter can work on almost all requests.
4. the interceptor can access objects in the action context and value stack, but the filter cannot
5. In the lifecycle of an action, the interceptor can be called multiple times, and the filter can only be called once during container initialization.
--------------------------------------------------------------------------------
The Interceptor is programmed in the aspect-oriented way by calling a method before your service or method, or by calling a method after the method. Is a reflection mechanism based on JAVA.
 
Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable
{
Object result = null;
System. out. println ("before invoke method:" + method. getName ());
Result = method.invoke(this.tar getObj, args );
System. out. println ("after invoke method:" + method. getName ());
Return result;
}
 
The filter is in java web. The request and response you pass in filter out some information in advance, or set some parameters in advance, and then input the servlet or struts action for business logic, for example, filter out invalid URLs (not login. do address requests are filtered out if the user does not log in), or set the character set before the servlet or struts action is passed in, or remove some illegal characters (frequently used in chat rooms, ).
 

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.