Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between servlet and filter in springMVC principle

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces what is the difference between servlet and filter in the principle of springMVC. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Recently, I used struts2 to do the project, and I used to use struts1 and springMVC before. I feel that struts2 is very different from these two to a great extent, so I collected some data today to compare them.

Struts1 has officially stopped updating and is now used less. Here we mainly talk about the differences and improvements in the comparison between struts2 and struts1. It can be said that Struts2 did not completely change from struts1, because struts2 is the world-famous Webwork2. After several years of development, the struts and WebWork communities decided to merge into one, that is, today's struts2.

New features of Struts2 compared with struts1:

Action class:

Struts1 requires that the Action class inherit an abstract base class. A common problem with Struts1 is programming with abstract classes rather than interfaces.

The Struts 2 Action class can implement an Action interface as well as other interfaces, making optional and customized services possible. Struts2 provides an ActionSupport base class to implement common interfaces. The Action interface is not required, and any POJO object identified by execute can be used as an Action object for Struts2.

Thread mode:

Struts1 Action is singleton and must be thread-safe because there is only one instance of Action to handle all requests. The singleton policy limits what Struts1 Action can do, and you have to be very careful when developing. Action resources must be thread-safe or synchronized.

The Struts2 Action object generates an instance for each request, so there is no thread safety problem. (in fact, the servlet container produces many disposable objects for each request and does not cause performance and garbage collection problems.)

Servlet dependencies:

Struts1 Action depends on ServletAPI because HttpServletRequest and HttpServletResponse are passed to the execute method when an Action is called.

Struts 2 Action does not depend on the container, allowing Action to be tested separately from the container. Struts2 Action can still access the original request and response if needed. However, other elements reduce or eliminate the need for direct access to HttpServetRequest and HttpServletResponse.

Testability:

One of the main problems with testing Struts1Action is that the execute method exposes servlet API (which makes the test dependent on the container). A third-party extension-Struts TestCase-- provides a set of Struts1 mock objects (for testing).

Struts 2 Action can be tested by initializing, setting properties, and calling methods, and dependency injection support makes testing easier.

Capture input:

Struts1 uses the ActionForm object to capture input. All ActionForm must inherit a base class. Because other JavaBean cannot be used as ActionForm, developers often create redundant classes to capture input. Dynamic Bean (DynaBeans) can be used as an option for creating a traditional ActionForm, but developers may be redescribing (creating) an existing JavaBean (which can still lead to redundant javabean).

Struts 2 directly uses the Action attribute as the input attribute, eliminating the need for the second input object. The input property may be a rich object type with its own (child) properties. The Action property can be accessed through taglibs on the web page. Struts2 also supports ActionForm mode. Rich object types, including business objects, can be used as input / output objects. This ModelDriven feature simplifies taglib's reference to POJO input objects.

Expression language:

Struts1 integrates with JSTL, so it uses JSTL EL. This EL has basic object graph traversal, but weak support for collection and index properties.

Struts2 can use JSTL, but it also supports a more powerful and flexible expression language-Object Graph Notation Language (OGNL).

Bind values to the page (view):

Struts 1 uses the standard JSP mechanism to bind objects to the page for access.

Struts 2 uses "ValueStack" technology to enable taglib to access values without having to bind your view to objects. The ValueStack policy allows pages to be reused (view) through a series of attributes with the same name but different types.

Type conversion:

The Struts 1 ActionForm attribute is usually of type String. Struts1 uses Commons-Beanutils for type conversion. One converter per class is not configurable for each instance.

Struts2 uses OGNL for type conversion. Provides converters for basic and commonly used objects.

Check:

Struts 1 supports manual verification in the validate method of ActionForm, or verification through an extension of Commons Validator. The same class can have different validation contents, but not child objects.

Struts2 supports validation through the validate method and the XWork verification framework. The XWork validation framework uses the checksum content checksum defined for the attribute class type to support chain checksum child attributes

Controls performed by Action:

Struts1 supports a separate Request Processors (life cycle) for each module, but all Action in the module must share the same life cycle.

Struts2 supports the creation of a different life cycle for each Action through the interceptor stack (Interceptor Stacks). The stack can be used with different Action as needed.

Comparison of SpringMVC and Struts2:

Mechanism:

The entry to spring mvc is servlet, while struts2 is filter. (to point out here, filter and servlet are different. It was previously thought that filter is a special kind of servlet), which leads to the difference between the two mechanisms, which involves the difference between servlet and filter.

Performance:

Spring will be slightly faster than struts. Spring mvc is method-based, while sturts is class-based. Each request will instantiate an action, and each action will be injected with attributes, while spring is method-based and finer-grained, but be careful as you control data in servlet. Spring3 mvc is a method-level interception that injects request data into the method according to the comments on the parameters. In spring3 mvc, a method corresponds to a request context. The struts2 framework is class-level interception, creating an Action every time a request comes, and then calling the setter getter method to inject the data in the request; struts2 actually deals with request through the setter getter method; in struts2, an Action object corresponds to a request context.

Parameter passing:

When struts accepts parameters, you can use attributes to accept parameters, which means that parameters are shared by multiple methods.

In terms of design ideas:

Struts is more in line with the programming ideas of oop, while spring is more cautious and extends on servlet.

The implementation mechanism of intercepter:

With its own interceptor mechanism, spring mvc uses an independent AOP approach. As a result, the number of configuration files of struts is still larger than that of spring mvc, although the configuration of struts can be inherited, so I think spring mvc is more concise in terms of use, and Spring MVC is indeed more efficient than struts2. Spring mvc is method-level interception, a method corresponds to a request context, and a method corresponds to a url at the same time, so spring3 mvc is easy to implement restful url from the architecture itself. Struts2 is a class-level interception, and a class corresponds to a request context; implementing restful url is laborious, because a method of struts2 action can correspond to a url; and its class properties are shared by all methods, so it is impossible to identify the method it belongs to by annotations or other means. Spring3mvc methods are basically independent, exclusive request response data, request data to be obtained through parameters, processing results returned to the framework methods through ModelMap do not share variables, and struts2 is quite messy, although the methods are also independent, but all its Action variables are shared, which does not affect the running of the program, but brings trouble to us when coding and reading the program.

Summary:

Strut1 is rarely used at present. I feel that springMVC is better than struts2.struts2 and springMVC in terms of ease of use and performance. Each camp has its own test data, so it's hard to say which is better. The above materials are extracted from the Internet, respect the copyright of the original author and share them with more readers.

Attachment: the difference between servlet and filter:

1. Concept:

1. Servlet:servlet is a server-side java application, which is independent of platform and protocol, and can generate web pages dynamically. It works in the middle layer between client request and server response.

2. Filter:filter is a reusable code snippet that can be used to transform HTTP requests, responses, and header information. Unlike Servlet, Filter cannot produce a request or response, it just modifies a request for a resource, or modifies a response from a resource.

Second, life cycle:

1. The life cycle of servlet:servlet begins when it is loaded into the memory of the web server and ends when the web server terminates or reloads the servlet. Once the servlet is mounted on the web server, it is generally not removed from the memory of the web server until the web server is shut down or re-terminated.

(1) load: load the instance of Servlet when you start the server

(2) initialization: start when the web server starts or when the web server receives the request, or at some point in between. The initialization work is executed by the init () method.

(3) call: the doGet () or doPost () method is only called for multiple visits from the first time to the next.

(4) destroy: when the server is stopped, the destroy () method is called to destroy the instance.

2. Filter: (be sure to implement the three methods init (), doFilter (), destroy () of the Filter interface of the javax.servlet package, or an empty implementation)

Load the instance of the filter when you start the server, and call the init () method to initialize the instance

(2). Only the method doFilter () is called for processing each request.

(3) when you stop the server, call the destroy () method to destroy the instance.

III. Responsibilities

1 、 servlet:

Create and return a complete html page that contains dynamic content based on the nature of the customer request

Create a partial html page (html fragment) that can be embedded in an existing html page

Read the hidden data sent by the client

Read the display data sent by the client

Communicate with other server resources, including database and java applications

Send hidden data to the client through the status code and the response header.

2 、 filter:

Filter can preprocess a user request before it reaches servlet, or it can process the http response when leaving servlet:

Before executing servlet, first execute the filter program and do some preprocessing work for it

Modify requests and responses according to the needs of the program

Intercept the execution of servlet after servlet is called

Fourth, the difference:

The url servlet process is short, and after it is sent, it is processed, and then returned or redirected to a self-specified page. It is mainly used to control before business processing.

2Different filter process is linear, after the url is sent, after checking, it can keep the original process to continue to execute, be received by the next filter, servlet, etc., but after servlet processing, it will not continue to pass down. The filter function can be used to keep the process going the way it was, or to dominate the process, while the function of servlet is mainly used to dominate the process.

Filter can be used to filter character encodings, to detect whether users have logged in, to disable page caching, and so on.

On the principle of springMVC servlet and filter what is the difference to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report