如何在 Java EE 中获取当前 Web 应用程序的名称?

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

How can I get the name of the current web app in Java EE?

javatomcatjakarta-ee

提问by Jenni

How can I get the name of the current web app in Java EE?

如何在 Java EE 中获取当前 Web 应用程序的名称?

I'm quite comfortable with stand-alone Java, but Java EE is new to me. I'm writing some custom code to plug in to a third-party Java EE reporting package. I have multiple instances deployed on the same Tomcat server, so I have something like:

我对独立的 Java 很满意,但 Java EE 对我来说是新的。我正在编写一些自定义代码来插入第三方 Java EE 报告包。我在同一个 Tomcat 服务器上部署了多个实例,所以我有类似的东西:

C:\
+-- tomcat6
    +-- webapps
        +-- app1
        +-- app2

So when the user goes to, let's say, http://example.com/app1/viewReport, I want to be able to get "app1". (And not by parsing the URL, preferrably.)

因此,当用户访问http://example.com/app1/viewReport 时,我希望能够获得“app1”。(最好不要解析 URL。)

Also, if there was a way to get the root of app1 (in this example, C:\tomcat6\webapps\app1), that would be great too.

此外,如果有办法获取 app1 的根目录(在本例中为 C:\tomcat6\webapps\app1),那也很棒。

回答by Hyman Leow

1. Getting the "name"

1. 获取“名称”

It's called the context path. If you code is running within the web app request context, you can get it by calling HttpServletRequest#getContextPath().

它被称为上下文路径。如果您的代码在 Web 应用程序请求上下文中运行,则可以通过调用HttpServletRequest#getContextPath().

2. Accessing the physical/real resource

2. 访问物理/真实资源

If you're trying to access the contents of a file/resource in your webapp, you're best of using one of:

如果您尝试访问 web 应用程序中文件/资源​​的内容,最好使用以下方法之一:

It is also possible get the physical path on disk of a file/resource, given the path relative to the web app, using ServletContext#getRealPath(String), but it's not reliable (doesn't always work if you deploy you webapp as a WAR, for instance).

考虑到相对于 Web 应用程序的路径,也可以使用 获取文件/资源​​磁盘上的物理路径ServletContext#getRealPath(String),但它并不可靠(例如,如果您将Web 应用程序部署为 WAR,则它并不总是有效)。

3. Accessing class path resources

3.访问类路径资源

Per your comment, you were trying to access a resource within the /WEB-INF/classes directory. Because WEB-INF/classes/* is where web application specific classes go, you can simply access it as if you were accessing any classpath resource in a Java SE application. Again, assuming your code runs within the context of the webapp, you can simply use the following:

根据您的评论,您试图访问 /WEB-INF/classes 目录中的资源。因为 WEB-INF/classes/* 是 Web 应用程序特定类所在的位置,所以您可以像访问 Java SE 应用程序中的任何类路径资源一样简单地访问它。同样,假设您的代码在 web 应用程序的上下文中运行,您可以简单地使用以下内容:

In your case, you'd probably want to use the latter, and then load the Properties file via Properties#load(InputStream).

在您的情况下,您可能希望使用后者,然后通过 Properties#load(InputStream) 加载属性文件。

Something along the lines of:

类似的东西:

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/reportCustom.properties"));

回答by McDowell

what i'm trying to do is load a properties file in webapps/myapp/WEB-INF/classes/reportCustom.properties

我想要做的是在 webapps/myapp/WEB-INF/classes/reportCustom.properties 中加载一个属性文件

Since the file is in the classesdirectory, you can load it using the ClassLoader(the usual Java mechanism for loading files on the classpath). It is probably best to use the context ClassLoader.

由于文件在classes目录中,您可以使用ClassLoader(在类路径上加载文件的常用 Java 机制)加载它。最好使用上下文 ClassLoader