In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the concept of Servlet 3.0API, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Today, web frameworks such as Struts, JSF, and Springweb are widely accepted and have been established as technologies for building web applications. Integrating these frameworks into a web application is not easy because the process involves assimilating different fragments together and then editing a separate descriptor file that describes how to fit all of them together. Most frameworks force you to configure framework details such as servlet classes (usually controller Servlet), filter classes, or listener classes in your application's deployment descriptor file. The main reason for this must-have configuration is that today's web applications only support a single overall deployment descriptor in which all deployment information is defined. As the size of the application increases, the dependencies of the external framework may also increase, resulting in complex deployment descriptor files. As you may know, the maintenance of complex descriptions is always controversial.
To solve these problems, one of the most prominent concepts of Servlet 3.0 API is the idea of web fragments or module web.xml. The Web fragment partitions the web application logic into elements such as servlet, servlet-mapping, servlet-filter, filter-mapping, servlet-listener, and their child elements. Framework developers can use this feature to define their own web fragments that exist within the framework, and developers can insert more and more frameworks simply by including library files in the application's classpath without modifying existing deployment descriptors. In short, this feature is designed to zero configuration when working with a framework or library.
The deployment descriptor has been changed to include this new element that holds the details of the web fragment. If the fragment is packaged as a .jar file and has metadata information in the form of a deployment descriptor, the web.xml file must be included in the META-INF directory of the .jar file. At deployment time, the device scans the classpath of the application, looks for all web fragments and processes them. The metadata-complete flag discussed earlier controls the scanning of web fragments during application startup. A sample web snippet is shown below:
Myservlet samples.MyServlet samples.MyListener
To provide enhanced pluggability, Servlet 3.0 provides much-needed support for adding servlets and filter classes to programming with the help of API added to ServletContext. These new API allow you to programmatically declare servlets, filter classes, and their URL mappings. These classes are initialized during the startup or operation of the application. Most importantly, you can only call these API through the contextInitialized side of ServletContext. For more information about these API, see the Servlet 3.0 API documentation. An example of code to add servlet and filter classes programmatically is as follows:
@ ServletContextListener public class MyListener {public void contextInitialized (ServletContextEvent sce) {ServletContext sc = sce.getServletContext (); / / Declare servlet and servlet mapping sc.addServlet ("myServlet", "Sample servlet", "samples.MyServlet", null,-1); sc.addServletMapping ("myServlet", new String [] {"/ urlpattern/*"}); / / Declare filter and filter mapping sc.addFilter ("myFilter", "Sample Filter", "samples.MyFilter", null) Sc.addFilterMapping ("myFilter", new String [] {"/ urlpattern/*"}, "myServlet", DispatcherType.REQUEST, false);}
You should all have encountered the problem of servlets running slowly, especially when servlets has to wait for responses from web services, JDBC connections, JMS messages, and so on. In the current case, servlet needs to wait for the process to complete before generating the response, which can lead to inefficient blocking operations that consume container threads or other limited resources. Another adverse effect when using JDBC connections is that there may be many blocked threads waiting for access to the database. This situation will eventually lead to a lack of threads and a decline in the quality of service of the entire web container.
To overcome these shortcomings, Servlet 3.0 adds support for pending and resume request processing, allowing servlet to respond to requests in an asynchronous, non-blocking manner (this is the programmatic Comet style). When a request is suspended, the thread processing the request returns to the container without generating any response and is ready to perform other tasks. The resume method that processes the request resumes request processing. As long as the requested resource is available, the thread handling the event resumes the pending request and processes it to generate a response. Here are some of the features of asynchronous servlets:
Even if the data arrives slowly (non-blocking input), it can receive data from the client without blocking events.
Even if the client or network runs slowly (non-blocking output), you can send data to the client without blocking.
Be able to handle delayed requests. It is useful to handle delayed events if you must obtain remote / slow resources before responding to a request, or if you need to suppress access to specific resources to prevent excessive synchronous access.
Can handle delayed response shutdown; that is, the response will remain open to allow additional data to be sent when an asynchronous event occurs.
Can notify blocking or non-blocking events.
Add a new API to ServletRequest and ServletResponse to suspend, restore, and query the status of requests, enable disables, and query responses. Developers can use the requested resume, suspend, and complete methods to notify events using the requestSuspended (), requestResumed (), and requestCompleted () methods, respectively. For more information about these methods, see Servlet 3.0 API.
This is the answer to the question on how to analyze the concept of Servlet 3.0API. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.