java 如何在 Tomcat 7 中创建虚拟目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5957052/
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
How do I create a virtual directory in Tomcat 7?
提问by Franky
Tomcat is installed at C:\tomcat7\ but I want to deploy .war files in C:\myapp\xyz. For example, I might have C:\myapps\xyz\MyApp.war and I should be able to reach it with the path http://localhost:8080/MyApp.
Tomcat 安装在 C:\tomcat7\ 但我想在 C:\myapp\xyz 中部署 .war 文件。例如,我可能有 C:\myapps\xyz\MyApp.war,我应该可以通过路径http://localhost:8080/MyApp 访问它。
I tried appending this to the bottom of c:\tomcat7\conf\server.xml
我尝试将其附加到 c:\tomcat7\conf\server.xml 的底部
<Host
name="myapp"
appBase="c:\myapps\xyz\"
unpackWARs="true"
autoDeploy="true">
</Host>
</Engine>
</Service>
</Server>
This doesn't seem to work though as I don't see MyApp listed in the management console and I am not able to hit the URL. What else do I need to do?
这似乎不起作用,因为我没有在管理控制台中看到 MyApp 列出,而且我无法访问 URL。我还需要做什么?
Also, slightly unrelated but, how can I not have the name of the war file tied to the context name or URL path? For example, I want http://localhost/coolNameto point to C:\myapps\xyz\MyApp.war.
另外,稍微不相关但是,我怎么能不将 war 文件的名称与上下文名称或 URL 路径相关联?例如,我希望http://localhost/coolName指向 C:\myapps\xyz\MyApp.war。
回答by laher
Unfortunately the way Tomcat loads a war by filename is a tricky limitation.
不幸的是,Tomcat 按文件名加载War的方式是一个棘手的限制。
I use a slightly different & OS-specific approach: "Symbolic Links". This isn't a direct answer, but may help you out anyway.
我使用了一种稍微不同且特定于操作系统的方法:“符号链接”。这不是一个直接的答案,但无论如何可能会帮助你。
Caveats:
注意事项:
- I use Linux, but this is
also possible in Windows Vista &
Windows 7. - using wars is still awkward using this method.
Wars are zip files, so best to
unzip your war into a
version-named folder.
- 我使用 Linux,但这
在 Windows Vista 和
Windows 7 中也是可能的。 - 使用wars使用这种方法仍然很尴尬。Wars 是 zip 文件,因此最好将您的 war 解压缩到以
版本命名的文件夹中。
Solution:
解决方案:
Create symbolic links (like a Virtual Directory on the filesystem) from your myapps folder into the webapps folder.
创建从 myapps 文件夹到 webapps 文件夹的符号链接(如文件系统上的虚拟目录)。
- This enables "coolName", "coolName-v2", etc.
- each webapp could potentially be kept in different places in the filesystem
- You can easily 'roll back' or 'upgrade' just by removing and re-adding symbolic links to different targets (make sure to "stop" the webapp while switching)
- 这将启用“coolName”、“coolName-v2”等。
- 每个 webapp 都可能保存在文件系统的不同位置
- 您只需删除并重新添加指向不同目标的符号链接即可轻松“回滚”或“升级”(确保在切换时“停止”webapp)
Linux:
Linux:
ln -s target_name link_name
Vista/Windows 7:
Vista/Windows 7:
mklink link_name target_name
In this way, you can still use c:\tomcat7\webapps\ , but specify symbolic links as follows:
这样,您仍然可以使用 c:\tomcat7\webapps\ ,但指定符号链接如下:
mklink c:\tomcat7\webapps\coolName\ c:\myapps\xyz\webapp123\
(Note: For wars, you'd need to unzip the war first)
(注意:对于War,你需要先解压War)
HTH
HTH
回答by Karthik Ramachandran
Your right in that you want to set the appBase paramete of the Host entry. Here is a previous question that discusses what it takes under windows : Apache Tomcat under Windows: Changing webapps default directory
您有权设置 Host 条目的 appBase 参数。这是上一个讨论在 Windows 下需要什么的问题:Windows下的Apache Tomcat:更改 webapps 默认目录
As for how to change name of the app add a context.xml in the META-INF directory: Separating war application name from war file name
至于如何更改应用程序的名称,请在 META-INF 目录中添加一个 context.xml:Separating war application name from war file name