| |---Context path--|--Servlet path-|--path info--|
http://www.myserver.com /mywebapp /helloservlet /hello
|--------Request URI --------------- -------------|
It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.
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" 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 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. In
This case, the path info is null.
2It tries to recursively match the longest path through stepping down the request URI
Path tree A directory in a time, using The/character as a path separator, and determining
If there is a the 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.
3If the last node of the request URI contains a extension (. jsp, for example),
The servlet container tries to match it to a servlet and handles requests for the
Specified extension. The complete request URI is the servlet path
and the path info is null.
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'll send a error message indicating
The servlet is 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/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 explains:
Here are three mistakes, all wrong on blue, note that blue mapping URL is/blue/rather than/blue or/blue/* this is the main cause of the error