4

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.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
Tim
  • 41
  • 1
  • 1
  • 2
  • By multiple entries for the context root (of a WAR), do you mean `/multiple/entries` vs `/singleentry` or something else? Actually, you are mixing web application concepts with servlet, it's very confusing. – Pascal Thivent Nov 28 '09 at 19:21

4 Answers4

2

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.

Mdhar9e
  • 1,376
  • 4
  • 23
  • 46
1

Yes. It's just a prefix. It must start with "/", and cannot end with "/" unless it is "/". Interior slashes are allowed.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
0

E.g. all Sun examples use a single "/catalog" or similar as the context root, but can it be "/catalog/furniture" as well?

In your web.xml, are you suggesting that you'd map "catalog/" and "catalog/furniture/" to the same servlet? What would be the point? I think the servlet engine will route both of these requests to the same servlet.

If you're suggesting that you've got two WAR files, one named catalog.war and another named something else, and both need to route all requests to the same servlet, either you'll have to have the .class file for that servlet in both WAR files, or the root servlet for the something else context will have to be written so it redirects all requests to the catalog.war root servlet.

Can you be a bit clearer as to what exactly you have in mind? I think what you'd like to accomplish is a little confusing.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I'm not sure but I think the question is about context paths (can it be `/catalog/furniture` for a WAR?), not servlet mapping. But the question is pretty unclear. – Pascal Thivent Nov 28 '09 at 18:59
  • Agreed, "/catalog" would be the default context if you have a catalog.war deployed. It seems like "catalog/furniture" would be redundant. It's almost as if the OP wants to have different categories in a catalog ("catalog/furniture" would be routed to the furniture.jsp, etc.) – duffymo Nov 28 '09 at 19:05
-1

No. You can't have slash in context root path.

However, you can achieve the same effect with context root as "/catalog". Just put all your static and JSP files under "/furniture" in the WAR and add map the Servlets accordingly.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • *"No. You can't have slash in context root path."* For my culture, what is the reference for this? – Pascal Thivent Nov 28 '09 at 21:35
  • In all implementations, context path is used as part of a file or directory name ("catalog.xml", "catalog.war" or "/webapps/catalog/"). Slash is not allowed in file/directory names for most file systems. – ZZ Coder Nov 28 '09 at 23:28
  • I have built application with a backslash in the context root before. I dont believe this is nesscarily true. It might be implementation specific. – gbtimmon May 30 '12 at 18:04