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, ).