File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes web app name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "web app name" Watch "web app name" New topic
Author

web app name

Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

And if you need this value in a JSP, it's as easy as:



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tarun Gandhi
Greenhorn

Joined: Jan 13, 2003
Posts: 13
{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

Joined: Jul 30, 2005
Posts: 96
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

Joined: Jan 10, 2002
Posts: 60782
    
  65

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

Joined: Jul 30, 2005
Posts: 96
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

Joined: Dec 11, 2004
Posts: 13410

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.
 
 
subject: web app name
 
Similar Threads
Authentication using Sun Access Manager 7 and webmethods portal
Tomcat 404 error
Default Web Application
Tomcat Error 404
No Response After Deployment