Tomcat principle and the process of processing HTTP requests, contextpath servletpath

Source: Internet
Author: User

I. Tomcat

1-components of Tomcat server

1.1-Server

A server element represents the entire Catalina servlet container. (Singleton)

1.2-Service

A service element represents the combination of one or more connector components that share a single engine

A service is a collection composed of one or more ctor s and an engine that processes customer requests obtained by all connector S.

1.3-connector

A connector listens to customer requests on a specified port, sends the obtained requests to the engine for processing, obtains responses from the engine, and returns the customer

Tomcat has two typical ctor, one directly listening for HTTP requests from browser and the other listening for requests from other webservers.

Coyote HTTP/1.1 Connector listens for HTTP requests from the client browser at port 8080

Coyote JK2 connector listens to Servlet/jsp proxy requests from other webserver (APACHE) at Port 8009

1.4-engine

The engine element represents the entire request processing machinery associated with a participant Service

It has es and processes all requests from one or more connectors

And returns the completed response to the connector for ultimate transmission back to the client

Multiple virtual hosts can be configured in the engine. Each virtual host has a domain name.

When the engine obtains a request, it matches the request to a host, and then delivers the request to the host for processing.

The engine has a default virtual host. When the request cannot match any host, the default host is used for processing.

1.5-host

Represents a virtual host. Each virtual host matches the domain name of a network domain.

Deploy one or more web apps under each virtual host. Each web app corresponds to a context and has a context path.

When the host receives a request, it matches the request to a context, and then sends the request to the context for processing.

The matching method is "Longest match", so a context with Path = "" will become the default context of the host.

All requests that cannot match other context path names will eventually match the default context.

1.6-Context

A context corresponds to a web application. A Web application consists of one or more servlets.

Context will load the servlet class according to the configuration file $ catalina_home/CONF/Web. xml and $ webapp_home/WEB-INF/Web. xml at creation

When the context obtains the request, it searches for the matching servlet class in its own mapping table.

If yes, this class is executed to obtain the request response and return

Assume that the request from the customer is:

HTTP: /localhost: 8080/wsota/wsota_index.jsp

1) The request is sent to the local port 8080, which is obtained by coyote HTTP/1.1 Connector

2) connector submits the request to the engine of the Service to process it, and waits for a response from the engine.

3) The engine obtains the request localhost/wsota/wsota_index.jsp, matching all the virtual host hosts it owns.

4) The engine matches the host named localhost (the request is handed over to the host even if the match fails, because the host is defined as the default host of the engine)

5) the localhost obtains the request/wsota/wsota_index.jsp and matches all the context

6) The host matches the context in the/wsota path. (if no match is found, submit the request to the context with the path name "" for processing)

7) obtain the request/wsota_index.jsp from the context of Path = "/wsota" and find the corresponding servlet in its mapping table.

8) the context matches the servlet whose URL pattern is *. jsp, which corresponds to the jspservlet class.

9) construct the httpservletrequest object and the httpservletresponse object, and call the jspservlet doget or dopost method as parameters.

10) Context returns the httpservletresponse object after execution to the host

11) The host returns the httpservletresponse object to the engine.

12) the engine returns the httpservletresponse object to Connector

13) connector returns the httpservletresponse object to the client browser.

 

2. Context path, Servlet Path, and Path Info

                        |-- Context Path --|-- Servlet Path -|--Path Info--|
http://www.myserver.com /mywebapp /helloServlet /hello
|-------- Request URI ----------------------------|

Remember the following three points:

1. Request URI = context path + Servlet Path + Path Info.

2. Context paths and Servlet paths start with A/but do not end with it.

3. httpservletrequest provides three methods getcontextpath (),

Getservletpath () and getpathinfo () to retrieve the context path,

The Servlet Path, And the Path Info, respectively, associated with a request.

Identifying the Servlet Path

To match a request URI with a servlet, the servlet container follows a simple algorithm.

Once it identifies the context path, if any, it evaluates the remaining part of

Request URI with the servlet mappings specified in the deployment descriptor, in

Following order. If it finds a match at any step, it does not take the next step.

1

The container tries to match the request URI to a Servlet Mapping. If it finds

Match, the complete request URI (except t the context path) is the Servlet Path. In

This case, the Path Info is null.
2

It tries to recursively match the longest path by stepping down the request URI

Path Tree A directory at a time, using the/character as a path separator, and determining

If there is a match with a servlet. If there is a match, the matching part

Of the request URI is the Servlet Path and the remaining part is the Path Info.
3

If the last node of the request URI contains an extension (. jsp, for example ),

The servlet container tries to match it to a servlet that handles requests for

Specified extension. In this case, the complete request URI is the Servlet Path

And the Path Info is null.
4

If the container is still unable to find a match, it will forward the request to

Default servlet. If there is no default servlet, it will send an error message indicating

The servlet was not found.

<Servlet-mapping>

<Servlet-Name> redservlet </servlet-Name>

<URL-pattern>/red/* </url-pattern>

</Servlet-mapping>

<Servlet-mapping>

<Servlet-Name> redservlet </servlet-Name>

<URL-pattern>/red/* </url-pattern>

</Servlet-mapping>

<Servlet-mapping>

<Servlet-Name> redblueservlet </servlet-Name>

<URL-pattern>/red/blue/* </url-pattern>

</Servlet-mapping>

<Servlet-mapping>

<Servlet-Name> blueservlet </servlet-Name>

<URL-pattern>/blue/</url-pattern>

</Servlet-mapping>

<Servlet-mapping>

<Servlet-Name> greenservlet </servlet-Name>

<URL-pattern>/green </url-pattern>

</Servlet-mapping>

<Servlet-mapping>

<Servlet-Name> colorservlet </servlet-Name>

<URL-pattern> *. Col </url-pattern>

</Servlet-mapping>

Request URI Servlet used Servlet Path Info

/Colorapp/red redservlet/red null

/Colorapp/red/redservlet/red/

/Colorapp/red/AAA redservlet/red/AAA

/Colorapp/red/blue/AA redblueservlet/red/blue/AA

/Colorapp/red/AAA redservlet/red/AAA

/Colorapp/AA. Col colorservlet/AA. Col null

/Colorapp/Hello/AA. Col colorservlet/Hello/AA. Col null

/Colorapp/red/AA. Col redservlet/red/AA. Col

/Colorapp/blue none (error message)

/Colorapp/Hello/blue/none (error message)

/Colorapp/blue/mydir none (error message)

/Colorapp/blue/DIR/AA. Col colorservlet/blue/DIR/AA. Col null

/Colorapp/green greenservlet/green null

 




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.