(reproduced) (personal backup) The difference and execution order of filter and interceptor

From leeleeleelee 's blog

 

1.Filter filter only filters jsp files and does not filter action requests Solution Solution

: put the filter configuration in front of the struts2 configuration in web.xml.


2. The difference between interceptor and Filter 
Spring's interceptor has similarities with Servlet's Filter. For example, both are the embodiment of AOP programming ideas, and both can implement permission checking and logging. The difference is:

the scope of use is different: Filter is specified by the Servlet specification and can only be used in Web programs. The interceptor can be used not only in Web programs, but also in Application and Swing programs.

Specifications are different: Filter is defined in the Servlet specification and is supported by the Servlet container. The interceptor is in the Spring container and is supported by the Spring framework.

The resources used are different: like other code blocks, the interceptor is also a Spring component, managed by Spring, and configured in the Spring file, so it can use any resources and objects in Spring, such as Service objects, data sources, transaction management Etc., can be injected into the interceptor through IoC; Filter cannot.

The depth is different: Filter works only before and after Servlet. The interceptor can go deep into the front and back of the method, before and after the exception is thrown, etc., so the use of the interceptor has greater flexibility. Therefore, in the program of the Spring framework, the interceptor should be used first.

This article comes from http://90songjian.blog.51cto.com/2264714/649596

 

In fact, Filter and Servlet are very similar, the difference is that Filter cannot directly generate a response to the user. In fact, the code in the doFilter() method in the Filter is the general code extracted from the service() methods of multiple servlets, and better reuse can be achieved by using the Filter. 

A filter is a reusable code snippet that can be used to transform HTTP requests, responses and headers. Unlike Servlet, Filter cannot generate a request or response, it just modifies the request for a resource, or modifies the response from a certain resource.  

It is stated in the JSR that according to multiple matching Filters, it is executed according to the order in which it is configured in web.xml.  

So this is the reason why you put your own Filter or other Filter (such as UrlRewrite's Filter) in front of Struts' DispatcherFilter. Because, they need to do some pre-work before the request is processed by the Struts2 framework. 

When the Filter is called and enters the DispatcherFilter of Struts2, Struts2 will call the Interceptor in the order of the Interceptors in the Interceptor Stack configured in the Action.
 
http://www.cnblogs.com/Fskjb/archive/2010/03/27/1698448.html  



3. Execution order of servlet, filter, interceptor



    Filter code:

Java code   收藏代码
  1. @Override  
  2.     public void doFilter(ServletRequest servletrequest,  
  3.             ServletResponse servletresponse, FilterChain filterchain)  
  4.             throws IOException, ServletException {  
  5.         System.out.println("in  filter 1.");  
  6.         filterchain.doFilter(servletrequest, servletresponse);  
  7.         System.out.println("outing filter 1");  
  8.     }  

 

 

   interceptor代码:
    @Override

Java代码   收藏代码
  1. public String intercept(ActionInvocation actioninvocation) throws Exception {  
  2.     System.out.println("in logininterceptor");  
  3.     String result=actioninvocation.invoke();  
  4.     System.out.println("outing logininterceptor");  
  5.     return result;  
  6. }  

 
    action代码:

Java代码   收藏代码
  1. @Override    
  2.    public String execute() throws Exception {    
  3.        System.out.println("in loginaciton");  
  4.        ActionContext context=ActionContext.getContext();  
  5.        Map<String, Object> session=context.getSession();   
  6.        session.put("userName", userName);    
  7.          
  8.       /* HttpServletRequest request = ServletActionContext. getRequest(); 
  9.        HttpSession session = request.getSession(); 
  10.        session.putValue("userName", userName);*/  
  11.        System.out.println("outing loginaciton");  
  12.        return SUCCESS;    
  13.    }   

 
      

   jsp代码:

Html代码   收藏代码
  1.  <script type="text/javascript">     
Html代码   收藏代码
  1. function submitForm(){  
  2.       document.getElementById("form1").submit();   
  3.    }  
  4. </script>  
  5.   
  6.  </head>  
  7.    
  8.  <body>  
  9.    This is Login page. <br>  
  10.    <form action="<%=path %>/login2.action" method="post" id="form1" name="form1">  
  11.       UserName:<input type="text" id="userName" name="userName"/><input type="button" value="submit" onclick="submitForm()" id="submit1" />  
  12.    </form>  
  13.  </body>  

 


   struts.xml

Xml代码   收藏代码
  1.  <struts>  
Xml代码   收藏代码
  1. <package name="default" extends="struts-default" namespace="/">    
  2.            
  3.          <interceptors>  
  4.             <interceptor name="MyInterceptor" class="Login.LoginInterceptor"></interceptor>  
  5.                 <interceptor-stack name="myInterceptorStack">  
  6.                     <interceptor-ref name="MyInterceptor"/>  
  7.                     <interceptor-ref name="defaultStack"/>  
  8.                 </interceptor-stack>  
  9.         </interceptors>  
  10.            
  11.             <action name="login2" class="Login.LoginAction">    
  12.                 <result name="success">    
  13.                    /Login/success.jsp    
  14.                 </result>  
  15.                 <interceptor-ref name="myInterceptorStack"></interceptor-ref>  
  16.             </action>    
  17.    
  18.         </package>  
  19.     </struts>  

 
    
console:
in  filter 1.
in logininterceptor
in loginaciton
outing loginaciton
outing logininterceptor
outing filter 1

 

3.servlet、filter的执行顺序

servlet代码:

 

Java代码   收藏代码
  1. public void init() throws ServletException {   
  2.     System.out.println("servlet初始化");  
  3.   }   

  

Java代码   收藏代码
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)          
Java代码   收藏代码
  1. throws ServletException, IOException {   
  2.       System.out.println("in servlet");  
  3.     response.setContentType("text/html");   
  4.     PrintWriter out = response.getWriter();   
  5.     out   
  6.         .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");   
  7.     out.println("<HTML>");   
  8.     out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");   
  9.     out.println("    <BODY>");   
  10.     out.print("        This is ");   
  11.     out.print(this.getClass());   
  12.     out.println(", using the POST method");        
  13.        
  14.     out.println("<br>");   
  15.     String x = request.getParameter("x");   
  16.     String y = request.getParameter("y");   
  17.     out.println("x="+x);   
  18.     out.println("<br>");   
  19.     out.println("y="+y);   
  20.     out.println("<br>");   
  21.        
  22.     out.println("    </BODY>");   
  23.     out.println("</HTML>");   
  24.     out.flush();   
  25.     out.close();   
  26.     System.out.println("outing servlet");  
  27.   }   

 

Java代码   收藏代码
  1. public void destroy(){        
Java代码   收藏代码
  1. System.out.println("servlet销毁");  
  2.         super.destroy();  
  3.     }  

 console:

 

servlet初始化

in  filter 1.

in servlet

before HttpServletRequest

after HttpServletRequest

outing servlet

outing filter 1

当tomcat容器停止的时候,输出:servlet销毁

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327090802&siteId=291194637
Recommended