1) The difference between servlet and filter
Filter can be considered as a "variant" of the servlet, which is mainly used to preprocess user requests or post-process httpservletresponse, which is a typical processing chain. It differs from the servlet in that it cannot generate a response directly to the user. The complete process is that the filter pre-processes the user request, then gives the request to the servlet for processing and generates a response, and finally the filter then processes the server response.
2) servlet, filter, interceptor execution sequence can be seen in the diagram above, the order should be we match the filter in front, followed by the Struts2 Filterdispatcher, and then the various interceptors, the last original path back.
Filter has several uses. Intercept the client's httpservletrequest before HttpServletRequest arrives at the servlet. You can also modify the HttpServletRequest header and data as needed to check the httpservletrequest. Intercept HttpServletResponse before HttpServletResponse arrives at the client. You can also modify the HttpServletResponse header and data as needed to check the httpservletresponse.
comparison of Springmvc and Struts2:
mechanism:
The entrance to Spring MVC is the servlet, and Struts2 is the filter (it is noted here that the filter and servlet are different.) Previously thought that filter was a special type of servlet, which led to a different mechanism between the two, which involved the difference between servlet and filter.
Performance:
Spring will be a little faster than struts. Spring MVC is a method-based design, and Sturts is class-based, each time a request is made with an action, each action is injected into the attribute, and spring is based on a method that is finer grained, but be careful to hold the same data as the servlet. SPRING3 MVC is a method-level interception that, after intercepting a method, injects the request data into a parameter based on annotations, and in Spring3 MVC, a method corresponds to a request context. The STRUTS2 framework is a class-level intercept, creating an action every time a request is made, and then invoking the setter getter method to inject the data from the request; Struts2 actually deals with the request through the setter getter method. ; in Struts2, an Action object corresponds to a request context.
parameter passing:
Struts is a parameter that can be accepted with attributes when it accepts parameters, which means that the parameters are shared by multiple methods.
Design Ideology:
Struts is more consistent with OOP programming ideas, and spring is more cautious and extensible on the servlet.
the implementation mechanism of Intercepter:
With its own interceptor mechanism, Spring MVC uses an independent AOP approach. This leads to the profile of struts is still larger than spring MVC, although the configuration of struts can inherit, so I think in terms of use, Spring MVC use more concise, development efficiency Spring MVC is really higher than struts2. Spring MVC is a method-level intercept, a method that corresponds to a request context, and a method that corresponds to a URL, so it is easy to implement a restful URL spring3 mvc from the schema itself. STRUTS2 is class-level interception, a class corresponds to a request context, and implementing a restful URL is laborious because a method of struts2 action can correspond to a URL, and its class properties are shared by all methods. It is also not possible to identify the method that it belongs to by annotations or other means. The Spring3mvc method is basically independent, the request response data, requests the data through the parameter acquisition, the processing result passes modelmap to the frame method not to share the variable between, but Struts2 is disorderly, although the method is also independent, But all of its action variables are shared, which does not affect the program's operation, but it gives us code and trouble reading the program.