简体   繁体   English

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

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

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. 我对独立Java很满意,但Java EE对我来说是新手。 I'm writing some custom code to plug in to a third-party Java EE reporting package. 我正在编写一些自定义代码来插入第三方Java EE报告包。 I have multiple instances deployed on the same Tomcat server, so I have something like: 我在同一个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". 因此,当用户访问时,请说http://example.com/app1/viewReport ,我希望能够获得“app1”。 (And not by parsing the URL, preferrably.) (而不是通过解析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),那也很棒。

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: 如果您尝试访问webapp中的文件/资源​​的内容,则最好使用以下方法之一:

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)也可以获取文件/资源​​的磁盘上的物理路径,但它不可靠(如果将webapp部署为webapp,则无法始终工作)例如,WAR)。

3. Accessing class path resources 3.访问类路径资源

Per your comment, you were trying to access a resource within the /WEB-INF/classes directory. 根据您的评论,您试图访问/ WEB-INF / classes目录中的资源。 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. 因为WEB-INF / classes / *是Web应用程序特定类所在的位置,所以您可以像访问Java SE应用程序中的任何类路径资源一样访问它。 Again, assuming your code runs within the context of the webapp, you can simply use the following: 再次,假设您的代码在webapp的上下文中运行,您可以简单地使用以下内容:

In your case, you'd probably want to use the latter, and then load the Properties file via Properties#load(InputStream). 在您的情况下,您可能希望使用后者,然后通过Properties#load(InputStream)加载Properties文件。

Something along the lines of: 有点像:

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

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 classes directory, you can load it using the ClassLoader (the usual Java mechanism for loading files on the classpath). 由于该文件位于classes目录中,因此可以使用ClassLoader (用于在类路径上加载文件的常用Java机制)加载它。 It is probably best to use the context ClassLoader . 最好使用上下文ClassLoader

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM