A Uniform Resource Locator (URL) is used to run a servlet. A Uniform Resource Identifier (URI) is a broader concept that includes both URLs and request-URI-paths. The request-URI-path follows the server name and optional port number. It starts with a forward slash.
For example, in the URL:
http://localhost:8080/filename.html /filename.html is the request-URI-path.
In a non-servlet-container web server like IIS or Apache without Tomcat, the basic handling of the request-URI-path is simple. The web content is rooted in a particular directory, so the web server can resolve that path,
C r e a t i n g a r u n t i m e c o n f i g u r a t i o n
using the leading slash to indicate the web-root directory. It can then return the corresponding file, if it’s there.
Servlet containers like Tomcat and WebLogic are more complex, but more flexible. These containers allow contexts and mappings, so that your web application can have any number of named contexts. Each context is mapped to its own root directory.
The servlet container’s first job in evaluating the request-URI-path is to see if the first part of the path matches a context name. In JBuilder, these are the WebApp names. During a Web Run, those names are associated with WebApp root directories. (For more information on WebApps, see “The WebApp” on page 3-1.)
If there is a match, the first part of the request-URI-path becomes the context path. The remaining part of the path, starting with a slash, becomes the URL-path-to-map. If there is no match, the context path is an empty string, and the entire request-URI-path is considered the
URL-path-to-map.
For example, for a project with a single WebApp named myprojectwebapp, the request-URI-path /myprojectwebapp/subpackage/somename.jsp would be evaluated as follows:
• The context path would be /myprojectwebapp.
• The URL-path-to-map would be /subpackage/somename.jsp.
However, the request-URI-path /test/subpackage/somename.jsp would not contain any context path in the evaluation, since the only existing WebApp is myprojectwebapp. In this case, the context path would be empty and the URL-path-to-map would be the entire URI: /test/subpackage/somename.jsp Note that the context configuration is done in a server-specific way. However, the matching of the URL-path-to-map is done via the
servlet-mapping entries in each context’s standard WebApp deployment descriptor, the web.xml file. Each servlet-mapping has two parts: a
URL-pattern and a servlet-name.
There are three special kinds of URL-patterns:
Note The three trees at the bottom of the Launch URI dialog box roughly correspond to the three different kinds of mappings. For specifics on how these mappings work in the Launch URI dialog box, see “Creating a server runtime configuration” on page 10-5.
Table 10.2 URL patterns
Pattern Type Description
Path mapping Starts with / and ends with /*
Extension mapping Starts with *.
W o r k i n g w i t h w e b a p p l i c a t i o n s i n J B u i l d e r 10-11
C r e a t i n g a r u n t i m e c o n f i g u r a t i o n All other URL-pattern strings are used for exact matches only. When matching the URL-path-to-map, an exact match is tried first. For example, if the WebApp somewebapp includes a URL-pattern /test/jspname.jsp, the corresponding servlet would be used.
If there is no exact match, a path match is attempted, starting with the longest path. In the default context, the URL-pattern /test/* would be the first match.
If there is no path match, then an extension match is tried. The URL-pattern *.jsp would match both of the following two request-URI-paths: /testwebapp/subpackage/jspname.jsp and /myprojectwebapp/anyfolder/myjsp.jsp.
Finally, if there is no extension match, the servlet mapped to / (the default servlet) is used.
Most web servers already have some default mappings, again done in a server-specific way. For example, *.jsp would be mapped to a servlet by:
1 Taking the path that was matched (e.g. /sub/somename.jsp or /test/subpackage/somename.jsp)
2 Finding the corresponding file relative to the context’s web-root directory
3 Converting it to a servlet if not done already
4 Running that servlet
Another typical default mapping is /servlet/*, which is an invoker servlet. An invoker servlet:
1 Takes whatever follows /servlet/ as a class name
2 Tries to run that class as a servlet
The default servlet (the one mapped to the URL-pattern /):
1 Takes the URL-path-to-map (that didn’t map to anything)
2 Finds the corresponding file relative to the context’s web-root directory
3 Sends it to the browser
When you create a standard servlet in the Servlet wizard, it does the minimum needed to create a mapping. For example, with the name myproject.MyServlet, the Servlet wizard:
1 Creates a servlet with the servlet-name myservlet associated with the servlet-class myproject.MyServlet
2 Creates the URL-pattern /myservlet and maps that to the servlet-name myservlet
S e t t i n g r u n p r o p e r t i e s
If that was done for the WebApp test, then /test/myservlet would run the servlet class myproject.MyServlet within the WebApp context test. For more information on how the Servlet wizard creates a mapping, see
“Enter WebApp Details page” on page 5-6.
When you Web Run a servlet .java (or .class) file, the servlet invoker is used if no exact URL-pattern is found for that servlet class. For the previous example, a web run under the WebApp test would run /test/myservlet/myproject.MyServlet.