Java Web Application - Mapping URLs to Web Components (context-root, url pattern)

> Procedural Languages > Java

1 - About

When it receives a request, the web container determine the web component that should handle the request via the URL

Advertising

3 - URL

A URL path contains the context root and, optionally, a URL pattern:

http://host:port/context-root[/url-pattern]

The web container does the mapping

  • between the URL path and a web application via the context-root
  • between the URL path and a servlet via the url-pattern

4 - Management

4.1 - Setting the context-root

The context root is defined in the web app descriptor

  • Glassfish: sun-web.xml;
  • JBoss: jboss-web.xml;
  • Weblogic: weblogic.xml;
  • Tomcat: context.xml;
  • WebSphere: ibm-web-ext.xml.

Example: WEB-INF/weblogic.xml (Doc)

<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
 <context-root>/</context-root>
</weblogic-web-app>
Advertising

4.2 - Setting the URL pattern

You set the URL pattern for a servlet by using the @WebServlet annotation in the servlet source file.

For example, the following annotation specify the URL pattern as /myAlias:

@WebServlet("/myAlias")
public class myServlet extends HttpServlet {
    ...

This annotation indicates that the URL pattern /myAlias follows the context root. Therefore, when the servlet is deployed, it is accessible with the following URL:

http://host:port/context-root/myAlias

To access the servlet by using only the context root, specify “/” as the URL pattern.

lang/java/context-root.txt · Last modified: 2018/09/20 11:56 by gerardnico