Servlet2.5 and 3.0 differences (Servlet 3.0 new features detailed)

Source: Internet
Author: User
Overview of the new features of the Servlet 3.0

Servlet 3.0 is a member of the Java EE 6 specification system, published along with the Java EE 6 specification. This version, based on the previous version (Servlet 2.5), provides several new features to simplify the development and deployment of WEB applications. The introduction of several features made the developers very excited, but also received a voice of praise from the Java community: Asynchronous processing support: With this feature, the servlet thread no longer needs to block until the business process is finished before outputting the response and finally ending the servlet thread. After receiving the request, the Servlet thread can delegate the time-consuming operation to another thread to complete and return to the container without generating a response. This will greatly reduce the consumption of server resources and increase the speed of concurrent processing, for business processing is time-consuming. Added annotation support: This release adds a number of annotations to simplify the declaration of Servlet, filter, and Listener (Listener), which makes Web.xml deployment profiles no longer required to be selected from that version. Pluggable support: Developers familiar with STRUTS2 are sure to remember the integration features of their plug-ins with the various common frameworks, including Spring. Encapsulate the appropriate plug-ins into JAR packages and place them under the classpath, which can be loaded automatically by the STRUTS2 runtime. Now that the Servlet 3.0 provides a similar feature, developers can easily expand the functionality of existing WEB applications through Plug-ins without having to modify the original application.

We'll cover these new features in a step-by-pass study where the reader will be able to learn more about the changes to Servlet 3.0 and use it for day-to-day development work. Asynchronous processing Support

Before Servlet 3.0, the main workflow for a common servlet was roughly as follows: first, after the servlet receives the request, it may need to preprocess some of the data that is being carried, and then call some methods of the business interface to complete the business process; The Servlet thread ends by submitting a response based on the processing result. The second step of business processing is usually the most time-consuming, mainly in database operations, and other calls across the network, during which the Servlet thread remains blocked until the business method executes. In the process of processing the business, Servlet resources have been occupied and not released, for concurrent large applications, which may cause bottlenecks in performance. In this respect, it used to be a private solution to prematurely end the Servlet thread and release resources in a timely manner.

Servlet 3.0 has done pioneering work on this issue, and now by using the Servlet 3.0 asynchronous processing support, the previous servlet processing process can be adjusted to the following procedure: First, after the servlet receives the request, It may be necessary to preprocess the requested data first; The servlet thread then forwards the request to an asynchronous thread to perform business processing, and the thread itself returns to the container, at which point the servlet has not generated the response data, and after the asynchronous thread has finished processing the business, The response data can be generated directly (the asynchronous thread has a reference to the ServletRequest and Servletresponse objects), or the request is forwarded to the other Servlet. As a result, the Servlet thread is no longer stuck in a blocking state waiting for the business logic to be processed, but it can be returned immediately after the asynchronous thread is started.

The asynchronous processing attribute can be applied to both the servlet and the filter components, because the working mode of asynchronous processing and the normal working mode are essentially different in implementation, so by default, the Servlet and the filter do not turn on the asynchronous processing attribute, and if you want to use this attribute, You must enable it as follows: for the use of the traditional deployment description file (Web.xml) to configure the servlet and filter, servlet 3.0 adds <async-su to the <servlet> and <filter> tags Pported> a child label whose default value is false, and to enable asynchronous processing support, set it to true. In the case of a Servlet, the configuration is as follows:

1 2 3 4 5 < servlet > < servlet-name >demoservlet</servlet-name > < Servlet-class >FOOTMARK.SERVL Et. Demo servlet</servlet-class > < async-supported >true</async-supported > </Servlet >
For a servlet or filter configuration using the @WebServlet and @WebFilter provided with Servlet 3.0, both annotations provide the Asyncsupported property, which defaults to false, and to enable asynchronous processing support, only You need to set this property to True. Take @WebFilter For example, the configuration is as follows:

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.