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


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


A URL path contains the context root and, optionally, a 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


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="">

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:

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:


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

Powered by ComboStrap