Go The tomcat principle and the process of handling HTTP requests, ContextPath Servletpath

Source: Internet
Author: User

First, TOMCAT

Components of the 1-TOMCAT server

<Server><Service> <Connector/> <Engine> <Host><Context></Context></Host> </Engine> </Service></Server>

1.1- Server

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

  <server port= "8005" shutdown= "shutdown" debug= "0">

      <Server> attribute Meaning:
--------------------------------------------------------------
ClassName: Specifies the class that implements the Org.apache.catalina.Server interface, with the default value of Org.apache.catalina.core.StandardServer.
PORT: Specifies the ports on which the Tomcat server listens for the shutdown command. When you terminate the Tomcat service runtime, you must issue the shutdown command on the same machine as the Tomcat server. This property must be set.
Shutdown: Specifies the string that is sent to the Tomcat server's shutdown listener port when the Tomcat server is terminated. This property must be set.

1.2-Service

A Service element represents the combination of one or more Connector a. Share a single Engine
A service is a collection that consists of one or more connector, and an engine that handles all customer requests received by connector.
   The <Service> element is defined by the Org.apache.catalina.Service interface, which contains a <Engine> element, and one or more <Connector> elements, which < The connector> element shares a <Engine> element. For example, two <Service> elements are configured in the sample file

<service name= "Catalina">
name= "Apache">
The first <Service> handles all Web client requests that are received directly by the Tomcat server, and the second <Service> handles web client requests that are forwarded over by the Apache server.

<service    <Service>Attribute meaning:
--------------------------------------------------------------
ClassName: Specifies the class that implements the Org.apache.catalina.Service interface, with the default value of Org.apache.catalina.core.StandardService.
Name: Defines the name of the service.
1.3-Connector

A connector will listen for the customer request on a specified port and give the request to the engine to process, receive a response from the engine and return to the customer
Tomcat has two typical connector, one that listens directly to HTTP requests from browser, and one that listens for requests from other webserver
Coyote http/1.1 Connector listens for Http requests from customer browser at port 8080
Coyote JK2 Connector listens for 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 particular Service
It receives and processes all requests from one or more connectors
and returns the completed response to the Connector for ultimate transmission back to the client
The engine can be configured with multiple virtual hosts, each hosting a domain name
When the engine obtains a request, it matches the request to a host and then gives the request to the host to handle
The engine has a default virtual host that, when the request cannot be matched to any host, is given to the default host for processing

1.5-Host


Represents a virtual host, which matches each virtual host with a network domain name
One or more web apps can be deployed under each virtual host, each Web app corresponds to a context with a context path
When host obtains a request, it matches the request to a context and then gives the request to the context to process
The matching method is "longest match", so a path== "" context will be the default context for that host
All requests that cannot match the path names of other context will eventually match that default context

1.6-Context

A context corresponds to a Web application, and a Web application consists of one or more servlets
The context will be loaded into the Servlet class based on the profile $catalina_home/conf/web.xml and $webapp_home/web-inf/web.xml when it is created
When the context obtains the request, it looks for the matching servlet class in its own mapping table (mapping table)
If found, executes the class, obtains the requested response, and returns


Suppose the request from the customer is:
http://localhost:8080/wsota/wsota_index.jsp

1) The request is sent toNative port 8080That was there listening to the coyote http/1.1ConnectorGet

2) connector the request to the service where it is located.EngineTo deal with and wait for a response from the engine

3) engine gets request localhost/wsota/wsota_index.jsp, matches all the virtual hosts it ownsHost

4) The engine matches the host named localhost (even if it does not match the request to the host processing, because the host is defined as the engine's default host)

5) localhost host gets the request/wsota/wsota_index.jsp, matching all of its ownedContext

6) host matches to the context where the path is/wsota (if the match is not matched, the request is given to the context of the path named "" to process)

7) path= "/wsota" The context gets the request/wsota_index.jsp, in its mapping table to find the corresponding servlet

8) context matches to a servlet with a URL pattern of *.jsp, corresponding to the Jspservlet class

9) constructs the HttpServletRequest object and the HttpServletResponse object, calling Jspservlet's Doget or Dopost method as a parameter

) context returns the HttpServletResponse object to host after it has finished executing

) host returns the HttpServletResponse object to the engine

) engine returns the HttpServletResponse object to connector

Connector return the HttpServletResponse object to the customer browser

Execution process

Tomcat processing HTTP request processing (from Geek College )

Second, Context path, Servlet path, 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 does 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 servlet paths match
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 the
Request URI with the servlet mappings specified in the deployment descriptor, in the
Following order. If it finds a match at no step, it does not take the next step.

1The container tries to match the request URI to a servlet mapping. If it finds a
Match, the complete request URI (except the context path) is the servlet path. Inch
This case, the path info is null.Exact Match first
2It tries to recursively match the longest path by stepping 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.Longest match
3If the last node of the request URI contains a extension (. jsp, for example),
The servlet container tries to match it to a servlet this handles requests for the
Specified extension. The complete request URI is the servlet path
and the path info is null. extension matches, if the last paragraph of the URL contains an extension, the container will select the appropriate servlet based on the extension.
4If The container is still unable to find a match, it'll forward the request to the
The default servlet. If there is no default servlet, it would send an error message indicating
The servlet is not found.Default or Error

<servlet-mapping>
<servlet-name>RedServlet</servlet-name>
<url-pattern>/red/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RedServlet</servlet-name>
<url-pattern>/red/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 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/RED/AAA REDSERVLET/RED/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

Go The tomcat principle and the process of handling HTTP requests, ContextPath Servletpath

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.