3

I am developing a common artifact that can be used in multiple web applications. The developers will just have to add my listener class to their web.xml. In my listener, I have a need to get the web application name that my code is running under.

Although getServletContextName() will give a name, this name is the display-name coded in web.xml. I think display-name is optional so it is not reliable. So I would like to get the actual web application name. Is there a way to do this?

Thanks for looking.

2
  • and what is the actual application name if it is not the display-name? Commented Sep 7, 2012 at 7:55
  • 4
    Is it request.getContextPath(), you are looking for. It returns the name of the web application which you use to invoke. e.g. localhost:8080/test`. So, return value will be "test" Commented Sep 7, 2012 at 12:17

2 Answers 2

4

try with this code

String ApplicationName = ((HttpServletRequest) request).getContextPath()
                    .replace("/", "");
Sign up to request clarification or add additional context in comments.

Comments

-1

I found a way to do this....

new java.io.File(context.getRealPath("/")).getName()

The above gives the web app name i.e WAR name.

Hardik, Thanks for looking into this.

MaVRoSCy, The display-name can be anything. It doesn't have to be the web application name. For instance, all Maven web projects will have the default display-name as 'Archetype created web application' which I don't want to pick up.

1 Comment

This is not a reliable way. getRealPath() returns null when the WAR file is not expanded on local disk file system, but instead in memory. This is a server-specific configuration setting and not controllable from inside your webapp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.