Javaweb---filter, Listener, Interceptor summary __web

Source: Internet
Author: User

One, Filter:

Servlet enhanced version, the user request preprocessing, can also be httpservletresponse after processing, is a typical processing chain.

Use:

1, before HttpServletRequest reach the servlet, intercept the customer's HttpServletRequest

2, according to the need to check httpservletrequest, or modify HttpServletRequest head and data

3. Before HttpServletResponse arrives at the client, intercept HttpServletResponse

4, according to the need to check httpservletresponse, or modify HttpServletResponse head and data

Kinds:

1, the user authorized filter

2. Log Filter

3, responsible for decoding the filter

4, can change the xsltfilter of XML content

5, filter can be responsible for intercepting multiple requests or responses, a request or response can also be multiple filter interception

To create a filter

1. Create Filter Processing class

public class FilterDemo01 implements Filter {

     @Override public
   void init (Filterconfig filterconfig) throws servletexception {
     }
    @Override public
    void Dofilter (ServletRequest request, servletresponse response,
            Filterchain chain) throws IOException, servletexception {
          dosomething;
          Chain.dofilter (request, response);
    @Override public
    Void Destroy () {
     }
}

2. Configure filter in Web.xml file

<!--configuration filter-->
   <filter>
      <filter-name>FilterDemo01</filter-name>
          < filter-class>me.gacl.web.filter.filterdemo01</filter-class>
 </filter>
  
  <!--mapping Filter- >
<filter-mapping>
       <filter-name>FilterDemo01</filter-name>
      <!--"/*" means to intercept all requests-->
      <url-pattern>/*</url-pattern>
  </filter-mapping>

Filter has exactly the same lifecycle behavior as the servlet.


Second, Listener

Monitor various events within the Web application and make a deal

Servletapi the common listener interfaces given, custom listener to selectively implement them:

1, Servletcontextattributelistener

2, Servletcontextlistener

3, Httpsessionlistener

4, Httpsessionattributelistener

Steps to use listener

1, the definition listener implementation class

2, through the annotation or web.xml file configuration listener annotation: @WebListener

Xml:

<listener>  
    <listener-class>com.ee.listener.OnlineUserListener</listener-class>  
</ Listener>


Third, Interceptor

Interceptor dynamically intercepts the object invoked by the action, calling a method before and after the service or other method. It is implemented using Java dynamic Proxy and reflection mechanism, which is independent of servlet. The internal methods of interceptors implemented by different frameworks are different.

Steps to implement interceptors (modules):

L Business components, agents and intercepted objects;

L Agent processor, implements an object of Invocationhandler interface;

L Agent object, proxy object;

L Interceptor, ordinary JavaBean, which automatically intercepts and executes some of its own methods before or after calling the business method;

L Client, perform the entry of business processing.

Specific see: http://blog.csdn.net/qq_35246620/article/details/68484407 speak very detailed, no longer repeat.


SPRINGMVC Interceptor Basic structure:

Package org.springframework.web.servlet;  
Public interface Handlerinterceptor {  
    boolean prehandle (  
            httpservletrequest request, HttpServletResponse Response,   
            Object handler)   
            throws Exception;  
  
    void Posthandle (  
            httpservletrequest request, httpservletresponse response,   
            Object handler, Modelandview Modelandview)   
            throws Exception;  
  
    void Aftercompletion (  
            httpservletrequest request, httpservletresponse response,   
            Object handler, Exception Ex)  
            throws Exception;  
}   


Filters (filter) and Interceptor (Interceptor) Difference: http://jay-zhong.iteye.com/blog/1511919

1, filter based on the Dofilter callback function in the filter interface, interceptor is based on the reflection mechanism of Java itself;

2, filter is dependent on the servlet container, no servlet container can not callback Dofilter method, and interceptor is not related to the servlet;

3, filter filtering range than interceptor, filter filter In addition to the request through the wildcard can protect the page, pictures, files, and so on, and interceptor can only filter the request, only action, the action before the start, Ends after the action completes (if blocked, no action is performed);

4, filter filters are generally loaded at the time of the Init method declaration, and interceptor can be in the XML declaration is a guest request or user request to identify whether filtering;

5, Interceptor can access the action context, the value stack of objects, and filter does not; 6, in the life cycle of the action, the interceptor can be invoked multiple times, and the filter can only be invoked once when the container is initialized.





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.