Java servlet context root
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.
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.
Yes. It's just a prefix. It must start with "/", and cannot end with "/" unless it is "/". Interior slashes are allowed.
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.
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.
精彩评论