Filter and Interceptor Learning in STRUTS2

Source: Internet
Author: User
Tags java web

Interceptors are programmed on a tangent plane to call a method before your service or a method, or invoke a method after the method, such as a dynamic proxy is a simple implementation of the Interceptor, the filter is in the Java Web, you incoming request, Response filter out some of the information in advance, or set some parameters in advance, and then pass into the servlet or struts action for business logic, such as filtering out illegal URLs (not login.do address requests, if the user does not log on to filter out), Either set the charset uniformly before passing in the servlet or struts action, or remove some illegal characters.

The difference between interceptors and filters:

1, interceptors are based on the reflection mechanism of Java, and filter is based on function callback

2. The filter relies on the servlet container, while the interceptor does not depend on the servlet container

3. Interceptors only work on action requests, while filters can work on almost all requests

4. The interceptor can access the action context, the object in the value stack, and the filter cannot

5, in the life cycle of the action, the interceptor can be called multiple times, and the filter can only be called once when the container is initialized

Filter filters

That is to write a class of their own, let this class implemented in the filter interface, this interface has three methods.

Init (), DoFilter (), Destroy (), mainly on DoFilter (), you can write in this method what you want to do.

After writing this, the configuration in Web. XML is

Java code <filter> <filter-name>myFilter</filter-name> <filter-class> Package name + implementation of the filter interface class </FI lter-class> </filter> <filter-mapping> <filter-name>myFilter</filter-name> <url- Pattern>/*</url-pattern> </filter-mapping>



After you set the/* in <url-pattern>, that means that the code in the Dofilter method in the custom filter is automatically executed for each operation. This allows for appropriate filtering operations.

Interception device

1. Defining interceptors

A. Method one: Implementing the Interceptor Interface Java code    public class myinterceptor implements interceptor  {   private string hello;//defining interceptors with parameters        public  String gethello ()  {          return hello;        }    public void sethello (String hello)  {           this.hello = hello;        }     Public void destroy ()  {           // TODO Auto-generated method stub           system.out.println ("Destroy");       }         @Override        public void init ()  {          //automatically loaded at server startup            System.out.println ("Init");         system.out.println ("Hello");     }       @Override        public string  intercept (actioninvocation arg0)  throws Exception {            system.out.println ("inercept");           string result=arg0.invoke ();           System.out.println ("Finish");          return result;        }  }  

B. Method Two: Inherit the Abstractinterceptor class Java code public class MyInterceptor2 extends Abstractinterceptor {@Override publi          C String Intercept (Actioninvocation arg0) throws Exception {System.out.println ("intercept2");          String Result=arg0.invoke ();          System.out.println ("Finish2");       return result; }   }

C. Method Three: Inherit the Methodfilterinterceptor class, define the filter method of the Interceptor Java code public class MyInterceptor3 extends Methodfilterinterceptor {//filter party Method Interceptor @Override protected String dointercept (Actioninvocation arg0) throws Exception {Arg0.addpreres          Ultlistener (New MyListener ());//Register the listener inside the SYSTEM.OUT.PRINTLN ("Myinterceptor3");          String Result=arg0.invoke ();          System.out.println ("after Myinterceptor3");       return result; }   }

2, configuring interceptors

Once the above interceptor is implemented, the above interceptor can be reused in all actions that require control. In order to use this interceptor, first define the interceptor in the Struts.xml file, defining the Interceptor configuration fragment as follows: Java code    <!--defining interceptors-->           <interceptors>          <!- -Define an interceptor with parameters-->              <interceptor  name= "Myinterceptor" >               <param name= "Hello" >w

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.