However, I still have confusion regarding the term "context". Why can't the method getContextPath() be called getWebAppPath()? From the API doc of the method: Returns the portion of the request URI that indicates the context of the request. What actually does "context of the request" mean? Do both "web application" and "context" mean same here?
Let's look at the servlet spec (version 2.4)
SRV.11.1 Use of URL Paths
Upon receipt of a client request, the Web container determines the Web application to which to forward it. The Web application selected must have the the longest context path that matches the start of the request URL. The matched part of the URL is the context path when mapping to servlets.
Mapping of a request URL is done in two steps:
1) Map the URL to the web-application
2) Map the URL to the servlet inside the selected web-application
example:
You have three web-applications on your servlet-container, called
FooStuff,
FooStuffOne and
FooStuffTwo.
Step 1) The request URL is
http://localhost:8080/FooStuffOne/StartServlet/logon.do. Although all web-applications contain the same prefix (i.e.
http://localhost:8080/FooStuff) The longest path from the request URL that matches a web-application is /FooStuffOne. This /FooStuffOne is now called the
context path: from here you will map an URL to a Servlet.
Step 2) The remaining part of the request URL is now
/StartServlet/logon.do this is now used to map to a Servlet. The mapping is done with the web.xml of the
FooStuffOne web-application. The 4 rules for mapping are described below. The order of the mapping rules is important. If a URL can be mapped on rule 1 and rule 4, it will be mapped on rule 1
Map the URL to the Servlet
The following order is used to map an URL to a servlet. The URL is mapped against the <url-pattern>s of the <servlet-mapping> elements
1) Exact path - the URL
pattern exactly matches a servlet-mapping
2) Longest prefix path - the longest match is used
3) Extensions - like *.jsp
4) Default - "/" single slash indicates default (if others didn't succeed)
Note: the querystring (e.g. ?name="bla") of an URL is never part of the mapping strategy
http://localhost:8080/FooStuffOne/StartServlet/logon.do
?name="bla"
Regards,
Frits