简体   繁体   English

Java Servlet上下文根

[英]Java servlet context root

Is it permissible to have multiple entries as the context root of a servlet? 允许有多个条目作为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? 例如,所有Sun示例都使用单个/catalog或类似的上下文根,但是也可以是/catalog/furniture吗?

The Servlet 2.4 Specification is not specific here. Servlet 2.4规范在这里不是特定的。

Context Path : The path prefix associated with the ServletContext that this servlet is a part of. Context Path :与该Servlet所属的ServletContext关联的路径前缀。 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. 如果此上下文是植根于Web服务器URL名称空间基础的“默认”上下文,则此路径将为空字符串。 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. 我们不能将多个上下文根赋予Servlet。 But we can change dynamically with different names in server.xml as 但是我们可以在server.xml使用不同的名称动态更改为

<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(); 默认情况下,如果您使用请求对象作为req.getContextPath();来调用contextPath req.getContextPath(); it will get by default with slash(/) as /myproj . 默认情况下,它将以slash(/)作为/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. 允许使用内部斜线。

Eg 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? 在您的web.xml中,是否建议将“ catalog / ”和“ catalog / furniture / ”映射到同一servlet? What would be the point? 重点是什么? I think the servlet engine will route both of these requests to the same 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. 我认为您想要完成的工作有点令人困惑。

No. You can't have slash in context root path. 不可以。上下文根路径中不能包含斜线。

However, you can achieve the same effect with context root as "/catalog". 但是,使用上下文根可以实现与“ / catalog”相同的效果。 Just put all your static and JSP files under "/furniture" in the WAR and add map the Servlets accordingly. 只需将所有静态和JSP文件放在WAR中的“ / furniture”下,并相应地映射Servlet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 servlet上下文到tomcat 7 root - servlet context to tomcat 7 root spring profile / servlet上下文根 - spring profile / servlet context root Spring 根应用程序上下文和 servlet 上下文混淆 - Spring root application context and servlet context confusion 使用 POST 在 tomcat 根上下文中访问 java servlet 时出现错误 403 - Error 403 when accessing java servlet in tomcat root context using POST 在根上下文和servlet上下文中创建Spring控制器bean - Spring controller bean is created in root context AND in servlet context Servlet.service() for servlet [...] in context with path [/...] 抛出异常 [Servlet 执行抛出异常] 具有根本原因 - Servlet.service() for servlet [...] in context with path [/...] threw exception [Servlet execution threw an exception] with root cause WebSecurityConfigurerAdapter添加到非Servlet 3.0容器中的根上下文 - WebSecurityConfigurerAdapter add to root context in non Servlet 3.0 container JBoss AS 7:通过代码配置将Servlet映射到上下文根(“ /”) - JBoss AS 7: Map Servlet to Context Root (“/”) via Code Config Java Servlet异步上下文存在性查询 - Java servlet async context existential enquiry Java Servlet上下文和会话级变量 - Java Servlet Context and Session level variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM