tags:

views:

185

answers:

3

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

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

"/catalog/furniture" as well? The servlet spec http://cds-esd.sun.com/ESD6/JSCDL/servlet/2.4-fr/servlet-2%5F4-fr-spec.pdf?AuthParam=1259432348%5F056658a1be4c791239f676cd802b8eed&TicketId=B%2Fw5kx2BSV9CSh1GO1BdlAXq&GroupName=CDS&FilePath=/ESD6/JSCDL/servlet/2.4-fr/servlet-2%5F4-fr-spec.pdf&File=servlet-2%5F4-fr-spec.pdf 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."

+1  A: 

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

Laurence Gonsalves
A: 

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
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
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
A: 

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
*"No. You can't have slash in context root path."* For my culture, what is the reference for this?
Pascal Thivent
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