Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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:

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.)

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

like image 955
Jenni Avatar asked Jan 18 '10 18:01

Jenni


People also ask

What is a web application Java?

A Java web application is a collection of dynamic resources (such as Servlets, JavaServer Pages, Java classes and jars) and static resources (HTML pages and pictures). A Java web application can be deployed as a WAR (Web ARchive) file.

What is server name in Java?

In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.

Can you make a web app in Java?

Java provides some technologies like Servlet and JSP that allow us to develop and deploy a web application on a server easily. It also provides some frameworks such as Spring, Spring Boot that simplify the work and provide an efficient way to develop a web application.


2 Answers

1. Getting the "name"

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().

2. Accessing the physical/real resource

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

  • ServletContext#getResource(String)
  • ServletContext#getResourceAsStream(String)

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).

3. Accessing class path resources

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:

  • Class#getResource(String)
  • Class#getResourceAsStream(String)

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

Something along the lines of:

Properties props = new Properties();
props.load(getClass().getResourceAsStream("/reportCustom.properties"));
like image 139
Jack Leow Avatar answered Sep 19 '22 23:09

Jack Leow


what i'm trying to do is load a properties file in 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). It is probably best to use the context ClassLoader.

like image 38
McDowell Avatar answered Sep 19 '22 23:09

McDowell