Java Tomcat - 将项目文件夹设为 Web 根目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4044129/
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-08-14 10:53:46  来源:igfitidea点击:

Tomcat - making a project folder the web root

javajsptomcat

提问by Luca Matteis

I have this folder under Tomcat webapps/mysitewhich is where all my JSPs and other things are located. To access this folder I go to http://blah.com/mysiteand it works just fine. However (because of stylesheets and images statically connected to the root /) I have to make it so that when I go to http://blah.com/it will load the stuff inside webapps/mysite.

我在 Tomcat 下有这个文件夹,webapps/mysite这是我所有的 JSP 和其他东西所在的地方。要访问这个文件夹,我去http://blah.com/mysite并且它工作得很好。但是(因为样式表和图像静态连接到根/)我必须这样做,以便当我去http://blah.com/ 时它会加载里面的东西webapps/mysite

I've tried many different things including contexts and setting the absolute path in server.xml... nothing seems to work, whenever I go to http://blah.com/it still tries to load the ROOT folder... what's happening here?

我已经尝试了很多不同的事情,包括上下文和设置绝对路径server.xml......似乎没有任何工作,每当我去http://blah.com/它仍然尝试加载根文件夹......这里发生了什么?

采纳答案by JoseK

The solution I use is to set this in your Tomcat server.xml

我使用的解决方案是在您的 Tomcat 中设置它 server.xml

Add a <Context>element within the <Host>like below which sets your mysiteas the default web app. Note the empty path=""which makes it the default.

<Context><Host>下面的类似内容中添加一个元素,将您设置mysite为默认 Web 应用程序。请注意path=""使其成为默认值的空值。

<Context docBase="mysite" path="" /> 

Attributes of Context Container from the Tomcat docs:

docBaseYou may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.

pathAll of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts.

来自 Tomcat 文档的上下文容器的属性:

docBase您可以为此目录或 WAR 文件指定绝对路径名,或者相对于拥有主机的 appBase 目录的路径名。

path特定主机内的所有上下文路径必须是唯一的。如果您指定空字符串 ("") 的上下文路径,则您正在为此主机定义默认 Web 应用程序,它将处理所有未分配给其他上下文的请求。

See others who have had similar question and the similar answer here, hereand here

在此处此处此处查看其他有类似问题和类似答案的人

See also Apache Tomcat Configuration Reference - Context

另请参阅Apache Tomcat 配置参考 - 上下文

回答by Michael

There are a number of ways to make an application the root application. The simplest way is to just replace the contents of webapps/ROOT with the contents of your web application.

有多种方法可以使应用程序成为根应用程序。最简单的方法是将 webapps/ROOT 的内容替换为您的 Web 应用程序的内容。

For other solutions, please see the following website: http://wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application_.3F

对于其他解决方案,请参阅以下网站:http: //wiki.apache.org/tomcat/HowTo#How_do_I_make_my_web_application_be_the_Tomcat_default_application_.3F

回答by George Siggouroglou

You can rename your war from something.war == to ==> ROOT.war.
So, tomcat will unpack the war and will create the folder ROOT for it.
It is a trick that is working on tomcat 8 also.

您可以将您的War从something.war ==重命名为 ==> ROOT.war
因此,tomcat 将解压 war 并为其创建文件夹 ROOT。
这是一个也适用于 tomcat 8 的技巧。

回答by Mariano LEANCE

https://stackoverflow.com/users/1123501/george-siggouroglou's awnser works but lacks a step.

https://stackoverflow.com/users/1123501/george-siggouroglou的 awnser 可以工作,但缺少步骤。

  • delete ROOT and all items
  • copy the war to webapps as ROOT.war
  • 删除ROOT和所有项目
  • 将War复制到 webapps 作为 ROOT.war

Without the deletion, it may not work. Tested with docker.

如果不删除,它可能无法工作。用 docker 测试过。