Comparison and analysis of working principle between Javaweb and ASP

Source: Internet
Author: User
Tags httpcontext java web tomcat server

I. Overview

Regardless of the language of the development of the Web application, is to solve the problem is that the user input URL how to respond to the corresponding page, how to map the URL to the response class, Because of their own time to do the net is not short, but also the whole process of ASP is understood, so in self-study javaweb is also very curious about how to deal with Javaweb.

The working principle of ASP.

The following is an introduction to the ASP. Net Work flow (scarlet Letter) and my personal understanding. Here also to learn the recommendation of the ASP. A book <<asp.net the essence of >> This book is a very good book on the HTTP request process.

In IIS 6.0, for example, in worker process w3wp.exe, use Aspnet_ispai.dll to load the. NET runtime (if. NET run fashion is not loaded). IIS 6 introduces the concept of an application pool, a worker process that corresponds to an application pool. One application pool can host one or more web apps, and each Web app maps to an IIS virtual directory. As with IIS 5.x, each Web app runs in its own application domain.

Is the list of IIS application pools for my local computer.

The Red Line section also shows clearly that the application pool is associated with a worker process, contains one or more applications, and provides isolation between different applications. There are 2 apps in my local test application pool. The specific creation can refer to the Nginx Load Balancer Chapter http://www.cnblogs.com/5ishare/p/6129775.html.

If HTTP. SYS received the first access to the Web app, when the runtime is successfully loaded, an application domain (AppDomain) is created for the Web App by Appdomainfactory. Subsequently, a special run-time isapiruntime is loaded. Isapiruntime is defined in assembly system.web, and the corresponding namespace is System.Web.Hosting. Isapiruntime will take over the HTTP request.

The application domain here provides four important mechanisms.

1. Isolate different application domains directly without direct access

2. Uninstalling the loaded application set can only be uninstalled in the application domain

3. Security with application domain as a boundary security mechanism

4. Configure configuration with application domain boundaries

Isapiruntime will first create a Isapiworkerrequest object that encapsulates the current HTTP request, The Isapiworkerrequest object is passed to the ASP. NET Runtime: HttpRuntime, from this point on, the HTTP request formally enters the ASP. Based on the Isapiworkerrequest object, HttpRuntime creates a context object to represent the current HTTP request: HttpContext.

Isapiworkerrequest is a relatively low-level object, httpruntime after the request will be analyzed to disassemble, create HttpRequest, HttpResponse object, an HTTP request requires several objects, There are also httpserverutility types of objects that handle mapping between Web site virtual paths and server file systems, and in order to manage these objects, HttpContext is defined to uniformly handle the representation of parameters.

As HttpContext is successfully created, HttpRuntime uses HttpApplicationFactory to create new or acquire existing HttpApplication objects. In fact, ASP. NET maintains a pool of HttpApplication objects, HttpApplicationFactory Select available HttpApplication users from the pool to process HTTP requests, and then release them to the object pool when processing is complete. The httpapplicationfactory is responsible for processing the current HTTP request. HttpRuntime the creation of HttpContext succeeds, the HttpApplication object is created, and it is important to note the source of the HttpApplication object creation, One is to create a HttpApplication object pool fetch through HttpApplicationFactory, which is not quite the same as in the Java Web. The servlet in Javaweb is a singleton multi-threaded, creating only one through a Servlet object, responding to requests through a line pool.

During HttpApplication initialization, the corresponding HttpModule object is loaded and initialized according to the configuration file. For HttpApplication, when it handles different stages of an HTTP request, it triggers different events (event), and HttpModule's meaning is to register HttpApplication's corresponding event, Inject the required operations into the process of the entire HTTP request. Asp. NET many functions, such as authentication, authorization, caching, etc., are implemented by the corresponding HttpModule.

When the request is transferred to the ASP. NET pipeline, the final responsibility for processing the request is the HttpHandler object that matches the requested resource type, but before handler formally works, ASP will load and initialize all configured HttpModule objects first. HttpModule in the process of initialization, some functions are registered to HttpApplication corresponding events, then the corresponding events will be triggered at some stage of HttpApplication the entire request processing life cycle. Event handlers that are registered through HttpModule are also executed. All HttpModule implement the IHttpModule interface.

The HttpModule here has the function of filtering, which is a bit of a type with Javaweb filter, which can be used to do some processing such as permissions. The benefits of this design are also obvious, extensibility is strong, users can choose their own use.

The final completion of the processing of the HTTP request is implemented in another important object: HttpHandler. For different resource types, there are different httphandler. For example, the. aspx page corresponds to the HttpHandler of the. svc file for the SYSTEM.WEB.UI.PAGE,WCF. HttpHandler for System.ServiceModel.Activation.HttpHandler.

Httphander a bit like Javaweb servlet, the final processing or need them, and is the basis of the Web, Javaweb JSP eventually will be run with a Servlet object, ASP.

Third, the Javaweb work flow

The above-net is mainly about the pond problem, the following javaweb is mainly about the container problem.

Tomcat's container is divided into four tiers, and the container that really manages the Servlet is the context container, a context that corresponds to a WEB project. Here is a bit of an ASP. NET-like application domain.

A Web application corresponds to a context container, which is the servlet container of the servlet runtime, and when a Web application is added, a standardcontext container is created and the necessary parameters are set for the context container. One of the most important configurations is contextconfig, which will be responsible for parsing the entire WEB application configuration and finally adding the Context container to the parent container Host.

1. In fact, the process of adding a project to the Tomcat server with Eclipse is doing the work above. Note the following Red Line section, if server is started,publish changes immediately. When the servers start, changes to the context container are released immediately. Can be released immediately, mainly by the observer design mode of listener. Contextconfig inherits the Lifecyclelistener interface, so changes to the contextconfig will take effect immediately.

2.ContextConfig objects

On the Contextconfig object, we can think backwards, we create a servlet class and need to configure it in Web. Xml Servlet-name, SERVLET-CALSS, servlet-mapping, Init-param, how does the context container know their correspondence and initialization parameters? So the contextconfig appeared. So many initialization parameters and mappings are found, and the servlet, Filter, and listener are put into the container by these parameters and mappings.

3.Servlet Instantiation, initialization

The above although the servlet's various initialization properties are added to the context container through Contextconfig, the servlet is not instantiated and initialized, so it is still not available. If the Servlet's Load-on-startup configuration entry is greater than 0, it is instantiated when the Context container is started. And the instantiation is to find the corresponding class, for the JSP file is also servlet,servletclass is the conf/web.xml defined in the Org.apache.jasper.servlet.JspServlet. The Servlet object is only created and initialized once, and if multiple requests are accessed at the same time, fetching a thread from the thread pool or processing the user request with the Servlet object is a singleton multi-threaded, which poses a problem, a thread-safety issue, Accessing a global variable in a servlet class, in particular, results in a data error, so it is not possible to declare a global variable in a servlet class. If not, you need to Cheng the line.

Life cycle of 4.Servlet

The term life cycle is common in development, and Servlets are no exception.

Servlet life cycle: servlet Load---> Instantiation (init ())---> Services (Service ())---> Destroy (Destroy ()).

Loading and instantiation are done at Tomcat boot time, when the client initiates the request, the servlet calls the service () method to respond to the request, matches the request in the service () method, chooses to call Doget,dopost, and so on. And then into the corresponding method to invoke the logical layer of the method to achieve response to the customer.

Destroy (): Executes only once, and executes the method when the server-side stops and the servlet is unloaded. When a Servlet object exits its lifecycle, it is responsible for releasing the resource that is occupied. A servlet may produce additional threads when running the service () method, so it is necessary to confirm that the threads have been terminated or completed when the Destroy () method is called.

The inheritance relationship of 5.Servlet

Each servlet we create inherits the abstract HttpServlet, and HttpServlet inherits the Javax.servlet.GenericServlet. Genericservlet is a generic, non-protocol-specific servlet that implements the Servlet interface.

The servlet interface and Genericservlet are not specific to any protocol, and HttpServlet is a class specific to the HTTP protocol, so the service () method is implemented in HttpServlet and the request is ServletRequest, Servletresponse Strong to HttpRequest and HttpResponse.

Comparison and analysis of working principle between Javaweb and ASP

Related Article

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.