如何在 Tomcat 7.0 中设置 Web 应用程序的上下文路径 [英] How to set the context path of a web application in Tomcat 7.0

查看:74
本文介绍了如何在 Tomcat 7.0 中设置 Web 应用程序的上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以将我的 web 应用程序(或它的 WAR 文件)重命名为 ROOT,但这是一种糟糕的方法,恕我直言.现在我查看了 tomcat doc &它说

I know that I can rename my webapp (or it's WAR file) to ROOT but this is a terrible way to do it, IMHO. Now I checked out the tomcat doc & it says

不建议将元素直接放在server.xml 文件

It is NOT recommended to place elements directly in the server.xml file

所以我尝试使用它建议的另一种方法.

So I tried doing it another method that it suggested.

Individual Context 元素可以明确定义:在应用程序文件中/META-INF/context.xml 中的单个文件.

Individual Context elements may be explicitly defined: In an individual file at /META-INF/context.xml inside the application files.

所以我用下面的代码创建了一个 /META-INF/context.xml

So I created a /META-INF/context.xml with the following code,

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/"/>

但是在我重新启动服务器时部署后它仍然无法加载/"处的上下文,它仍然使用/<WEB_APP_NAME>"

But after deploying when I restarted the server it still failed to load the context at "/", it still loaded it with the "/<WEB_APP_NAME>"

任何有用的提示.

推荐答案

你可以做的是以下;

/conf/Catalina/localhost/

此 ROOT.xml 将覆盖该引擎和主机(Catalina 和 localhost)的 tomcat 安装的根上下文的默认设置.

This ROOT.xml will override the default settings for the root context of the tomcat installation for that engine and host (Catalina and localhost).

在 ROOT.xml 文件中输入以下内容;

Enter the following to the ROOT.xml file;

<Context 
  docBase="<yourApp>" 
  path="" 
  reloadable="true" 
/>

这里,<yourApp> 是您的应用程序的名称.. :)

Here, <yourApp> is the name of, well, your app.. :)

好了,您的应用程序现在是默认应用程序,并将显示在 http://localhost:8080

And there you go, your application is now the default application and will show up on http://localhost:8080

然而,有一个副作用;您的应用程序将被加载两次.一次用于 localhost:8080,一次用于 localhost:8080/yourApp.要解决此问题,您可以将应用程序放在 <catalina_home>/webapps 之外,并在 ROOT.xml 的 docBase 标记中使用相对或绝对路径.像这样;

However, there is one side effect; your application will be loaded twice. Once for localhost:8080 and once for localhost:8080/yourApp. To fix this you can put your application OUTSIDE <catalina_home>/webapps and use a relative or absolute path in the ROOT.xml's docBase tag. Something like this;

<Context 
  docBase="/opt/mywebapps/<yourApp>" 
  path="" 
  reloadable="true" 
/>

然后就一切正常了!

这篇关于如何在 Tomcat 7.0 中设置 Web 应用程序的上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆