Mapping Requests to servlets

xiaoxiao2021-03-06  63

This is a good time to digress for a moment and discuss how the URLs that a user types into a browser are mapped to the correct web application and servlet. When a web application is installed in a container, the container is responsible for assigning a ServletContext To it. There is a single instance of a servletcontext Object for Each Web Application Deployed in a Container.

IF The Container IS Distributable and Uses More Than One JVM, The Web Application May Have A Separate ServletContext Instance for Each JVM.

The ServletContext provides an external view of the web container environment for a servlet. A servlet can use the ServletContext object to gain access to external resources, log events, and store attributes and objects that other servlet instances in the same context can access. It's essentially An Application-Scope Shared Resource.

Because a servlet is associated with a specific web application, all requests that begin with a specific request path (known as the context path) are routed to the web application associated with that servlet. Servlets associated with the default application have an empty string ( " ") as the context path.

When a web container receives a client request, it must determine the correct web application to forward it to. The web container determines this by matching the URL with the longest context path that matches an installed web application.

For example, suppose that there are two web applications installed in a container. One web application is given the name Storefront and is located off the root directory of the container at / storefront. The second web application is called Storefront_ demo and is located off the Root Directory At / StoreFront_Demo.

If a client request arrives at the server with a URL of http://www.somehost.com/ storefront_demo / login.do, the server will match it to the web application that has the closest match, which in this case would be the Storefront_demo application. Once the container determines the correct context or web application, it must determine which servlet in the web application should process the request. The web container uses the request URL, minus the context path, to determine the path that will be used to Map The Request to the Correct Servlet.The Web Container Uses The Firlowing Guidelines To Find The First Successful Match:

The container attempts to locate an exact match of the request path to the path of a servlet. The container recursively tries to match the longest path prefix. The servlet that contains the longest match, if any, is selected. If the URL path contains an extension-for example, .do-the servlet container tries to match a servlet that handles requests for that extension. The extension is defined as the part of the segment after the last dot (.). If none of the previous rules produces a match The Container Attempts To Use a Default Servlet, IF One IS Configured. Otherwise, The Request Returns An Error Response.

The website.

The concept of extension mappings was mentioned in Step 3 of the matching guidelines. There is another type of mapping that can be used, known as path mapping. A servlet-mapping that uses path mapping allows a URL that does not contain an extension to Match to the servlet. using the earlier storefront servlet mapping, The follrowing partial web.xml file illustrate how path mapping is configured:

Storefront

org.apache.struts.Action.ActionServlet

Storefront

/ action / *

Uns..

转载请注明原文地址:https://www.9cbs.com/read-119437.html

New Post(0)