This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

web app name

 
Ra Carter
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using apache 5.5 and i want to know how to dynamically learn the name that my web application was deployed with. for example, if my web app was deployed as http://localhost/myapp i want to find the myapp part.

context.getContextPath does not seem to compile in a servlet and i am not sure if it would solve my problem in a java ee container either.

the main reason i want to know this information by the way is because i don't want links in my jsp pages to hard code the app name, so for example, links would say >img src="${rootDir}/img/pic.gif" /<.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that you mean Apache Tomcat 5.5.

The Appache foundation also supports the Apache Web Server, commonly referred to as "Apache", which does not host Java applications directly.

The name for what you're looking is contextPath.
You can read this, dynamically from the ServletRequaet object.

context.getContextPath does not seem to compile in a servlet


If you're developing Java web apps, you should really have a link to the API readily available.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html
This will save you a lot of time over guessing and trying to compile.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you need this value in a JSP, it's as easy as:

 
Tarun Gandhi
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{Edit "me too" post removed. Tarun Gandhi, your willingness to help is appreciated, but please do not post replies that just repeat what has already been said. Not only does it waste bandwidth, it may confuse newcomers who wonder if your answer is subtly different from a previous answer with the same information.]
[ August 04, 2007: Message edited by: Bear Bibeault ]
 
Ra Carter
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did mean apache tomcat 5.5, sorry. I am not sure what method i should be looking for in the api. In the Java EE 5 API, I was able to find a ServletContext.getContextPath() method but not in the J2EE 1.4 API provided in the previous post. Since I am using tomcat 5 i am not sure how to find this information in a context listener.

i was able to do this in a JSP with EL but it just seemed like a lot to type for each link and that's why i wanted to put it in an attribute once at the start. i could use jstl or something to set a variable but it just seems like the wrong way, i want to set the variable in my controller...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll get nowhere if you don't look at the javadoc for the API that you are using. Tomcat 5 supports Servlets 2.4 so be sure you are looking at an up-to-date api. Or look at the servlet specification.

In your context listener, the method is passed all the information it needs to get the information you seek.
 
Ra Carter
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it feels like i am doing it the wrong way...

the best i could do was to use context.getResource("/"); which returns "jndi:/localhost/myapp/". i figure the easiest thing to do with that is to parse that value for /myapp and set that as the root dir?

i also found context.getRealPath("/") which returns something equally terrible.

to find what i am looking for, which is the root directory of my web application, both methods involve me parsing the result. it feels like what i am doing is a hack but is this the technique i should use?

ServletContext.getServletContextName gives me the display name from the dd.

i am slow so perhaps i am missing something very obvious. if you know the method call i should be using then by all means please do feel free to tell me.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ra Carter:
... it feels like what i am doing is a hack ...


You're not alone:
http://www.coderanch.com/t/361361/Servlets/java/webapp-name-configured-Tomcat

The ServletContext.getContextPath method in the servlet spec is a nice addition. Personally, I think they should have done that when then introduced contextListeners.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic