The problem to be solved in this article: How does the container know which servlet to choose when an http request comes over?
We know that a Context container represents a web application, and a Wrapper container represents a servlet. Therefore, the question above can be converted to how to select a servlet from the Context container. The answer is. The er is a class that implements the ER interface. It is used to select the next container based on request connections (mainly protocols and paths). It can be seen as a hash table, select a specific value based on the keyword segment. The Mapper interface is defined:
[Java]
Public interface Mapper {
Public Container getContainer (); // return the Container associated with the iner
Public void setContainer (Container container );
Public String getProtocol (); // return the Protocol processed by the er.
Public void setProtocol (String protocol );
Public Container map (Request request, boolean update); // ing function to implement this function
}
In Tomcat source code analysis (4) -- container processing link's responsibility chain mode, we know that the invoke Method for connecting requests to the StandardContext container has the following code in the StandardContext container:
[Java]
Wrapper = (Wrapper) context. map (request, true );
This Code indicates that the container will call the map method to map the request to a specific wrapper, which means to select wrapper Based on the connection request. The above map will call the map method of the parent ContainerBase class to find the specific er. For details about how the er and container are associated, see Tomcat source code analysis (3) -- How is a connector associated with a container? The general principle of this article is the same. The StandardContext container has a standard er implementation class StandardContextMapper. Therefore, the map method of StandardContextMapper is called. This method is the key to servlet selection (some code is omitted ):
[Java]
Public Container map (Request request, boolean update ){
// Identify the context-relative URI to be mapped
String contextPath =
(HttpServletRequest) request. getRequest (). getContextPath ();
String requestURI = (HttpRequest) request). getDecodedRequestURI ();
String relativeURI = requestURI. substring (contextPath. length ());
// Apply the standard request URI mapping rules from the specification
Wrapper wrapper = null;
String servletPath = relativeURI;
String pathInfo = null;
String name = null;
// Rule 1 -- Exact Match
If (wrapper = null ){
If (debug> = 2)
Context. log ("Trying exact match ");
If (! (RelativeURI. equals ("/")))
Name = context. findServletMapping (relativeURI );
If (name! = Null)
Wrapper = (Wrapper) context. findChild (name );
If (wrapper! = Null ){
ServletPath = relativeURI;
PathInfo = null;
}
}
// Rule 2 -- Prefix Match
If (wrapper = null ){
If (debug> = 2)
Context. log ("Trying prefix match ");
ServletPath = relativeURI;
While (true ){
Name = context. findServletMapping (servletPath + "/*");
If (name! = Null)
Wrapper = (Wrapper) context. findChild (name );
If (wrapper! = Null ){
PathInfo = relativeURI. substring (servletPath. length ());
If (pathInfo. length () = 0)
PathInfo = null;
Break;
}
Int slash = servletPath. lastIndexOf ('/');
If (slash <0)
Break;
ServletPath = servletPath. substring (0, slash );
}
}
// Rule 3 -- Extension Match
If (wrapper = null ){
If (debug> = 2)
Context. log ("Trying extension match ");
Int slash = relativeURI. lastIndexOf ('/');
If (slash> = 0 ){
String last = relativeURI. substring (slash );
Int period = last. lastIndexOf ('.');
If (period> = 0 ){
String pattern = "*" + last. substring (period );
Name = context. findServletMapping (pattern );
If (name! = Null)
Wrapper = (Wrapper) context. findChild (name );
If (wrapper! = Null ){
ServletPath = relativeURI;
PathInfo = null;
}
}
}
}
// Rule 4 -- Default Match
If (wrapper = null ){
If (debug> = 2)
Context. log ("Trying default match ");
Name = context. findServletMapping ("/");
If (name! = Null)
Wrapper = (Wrapper) context. findChild (name );
If (wrapper! = Null ){
ServletPath = relativeURI;
PathInfo = null;
}
}
The code is very long, but it is easy to understand, that is, to select wrapper in the matching mode (full match, prefix match, extended match, default match) in 4. The key code is name = context. findServletMapping and wrapper = (Wrapper) context. findChild (name); the context is StandardContext. Context. findServletMapping is used to find the servlet name based on the matching mode. context. findChild is used to find the specific wrapper Based on the servlet name. The findServletMapping method is very simple, that is, get the servlet name in a HashMap. The Code is as follows, in which servletMappings is a HashMap:
[Java]
Public String findServletMapping (String pattern ){
Synchronized (servletMappings ){
Return (String) servletMappings. get (pattern ));
}
}
Like the findServletMapping method, the findChild method is also used to find the wrapper container in a HashMap. Now, you can answer the first question. The StandardContext container selects wrapper Based on the er. Of course, container Engine and Host also select their next-level containers based on the er.
Author: haitao111313