Java servlet 上下文根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1813343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:05:50  来源:igfitidea点击:

Java servlet context root

javajakarta-eecontextpath

提问by Tim

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

允许有多个条目作为 servlet 的上下文根吗?

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

例如,所有 Sun 示例都使用单个/catalog或类似的内容作为上下文根,但也可以这样/catalog/furniture吗?

The Servlet 2.4 Specificationis not specific here.

Servlet 2.4规范是不特定在这里。

Context Path: The path prefix associated with the ServletContextthat 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.

上下文路径:与ServletContext此 servlet所属的路径前缀相关联。如果此上下文是根植于 Web 服务器 URL 名称空间基础的“默认”上下文,则此路径将为空字符串。否则,如果上下文不是以服务器名称空间的根为根,则路径以“/”字符开头但不以“/”字符结尾。

回答by Mdhar9e

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

我们不能为 servlet 提供多个上下文根。但是我们可以在server.xmlas 中使用不同的名称动态更改

<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.

默认情况下,如果您使用请求对象调用 contextPath,因为req.getContextPath();默认情况下它将使用斜杠 (/) 作为/myproj

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

有关将上下文根从静态更改为动态的更多信息,您可以在此处查看

回答by Laurence Gonsalves

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

是的。这只是一个前缀。它必须以“/”开头,不能以“/”结尾,除非它是“/”。允许内部斜线。

回答by duffymo

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

例如,所有 Sun 示例都使用单个“/catalog”或类似的作为上下文根,但它也可以是“/catalog/furniture”吗?

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.

在您的 web.xml 中,您是否建议将“catalog/ ”和“catalog/furniture/”映射到同一个 servlet?重点是什么?我认为 servlet 引擎会将这两个请求路由到同一个 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.

如果您建议您有两个 WAR 文件,一个名为 catalog.war,另一个名为其他名称,并且都需要将所有请求路由到同一个 servlet,那么您必须为此拥有 .class 文件必须编写两个 WAR 文件中的 servlet 或其他内容的根 servlet,以便将所有请求重定向到 catalog.war 根 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.

你能更清楚一点,你到底有什么想法?我认为你想要完成的事情有点令人困惑。

回答by ZZ Coder

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.

但是,您可以使用上下文根实现与“/catalog”相同的效果。只需将所有静态和 JSP 文件放在 WAR 中的“/furniture”下,并相应地添加映射 Servlet。