This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Getting the appname from the web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting the appname from the web.xml" Watch "Getting the appname from the web.xml" New topic
Author

Getting the appname from the web.xml

Mark Wa
Ranch Hand

Joined: May 29, 2009
Posts: 122
In a java class within my app, how would I get the "appname" of my application module? (.war)

It looks like this in the web.xml


thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Getting context parameters is easy enough. Have a read of javax.servlet.ServletContext for the available methods.

Easier than setting a context param however is just getting it from the request.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Wa
Ranch Hand

Joined: May 29, 2009
Posts: 122
OK. so I could do that from a servlet, but how would I do it from a POJO?
and preferably in a static safe way?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Why do you need to do this from a POJO? And what do you mean by "static safe"?

If you have to, then I suppose you would need to parse web.xml yourself rather than having the servlet container do it for you. Bit of an odd requirement though.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41054
    
  43
Since the app name doesn't change over time, you can retrieve it at web app startup time (using a context listener), and store it in some object in a public static field.


Ping & DNS - my free Android networking tools app
Mark Wa
Ranch Hand

Joined: May 29, 2009
Posts: 122
sounds good.

I need it for printing to a log on a server. From a static method
 
 
subject: Getting the appname from the web.xml
 
Similar Threads
How to give a path name in web.xml
Spring with Quartz
ServletContext Null pointer Exception
stylesheet messed up in richfaces JSF page
JDBC Parameters as Context Parameters?