Java servlet context root

Tim picture Tim · Nov 28, 2009 · Viewed 12.6k times · Source

Is it permissible to have multiple entries as the context root of a servlet?

For instance, all Sun examples use a single /catalog or something similar as the context root, but could it be /catalog/furniture as well?

The Servlet 2.4 Specification is not specific here.

Context Path: The path prefix associated with the ServletContext that this servlet is a part of. If this context is the “default” context rooted at the base of the Web server’s URL name space, this path will be an empty string. Otherwise, if the context is not rooted at the root of the server’s name space, the path starts with a’/’ character but does not end with a’/’ character.

Answer

Mdhar9e picture Mdhar9e · Feb 14, 2012

WE can't give multiple context root to a servlet. But we can change dynamically with different names in server.xml as

<web-uri>Project.war</web-uri>
<context-root>myproj</context-root>

We can configure our project context root with different names but it should be one name.

By default if you call contextPath using request object as req.getContextPath(); it will get by default with slash(/) as /myproj.

For more info to change context roots as static to dynamic you can check here.