aspose file tools*
The moose likes Servlets and the fly likes Getting Application Root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting Application Root" Watch "Getting Application Root" New topic
Author

Getting Application Root

Prabhakar Rao
Ranch Hand

Joined: Aug 23, 2006
Posts: 40
Hello
I wanted to store a file in the root directory of current web application
When I am creating a file it stored in Tomcat5.5 Home directory
When I tried with eclipse it was stored in Eclipse Home
I need a way to get the Context home so that I need not change the code later.

-Prabhakar
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If your application is running from an exploded file system (as opposed to a packed war archive), you can use the context.getRealPath("/") method.
java.lang.String)" target="_blank" rel="nofollow">http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Prabhakar Rao
Ranch Hand

Joined: Aug 23, 2006
Posts: 40
That's fine in Servlets
But how can I get a Context in the ServletContextListener.
I want to read a file while the application starts.
-Prabhakar
Prabhakar Rao
Ranch Hand

Joined: Aug 23, 2006
Posts: 40
Hey sorry
I got the context from ServletContextListener.
Thanks for your post.
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Originally posted by Prabhakar Rao:
Hey sorry
I got the context from ServletContextListener.
Thanks for your post.


request.getContextPath() gives the root of the current Context i.e root of the current web application.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by vishwanath nadimpally:


request.getContextPath() gives the root of the current Context i.e root of the current web application.

Requests don't ever get passed to contextListeners.
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Originally posted by Ben Souther:

Requests don't ever get passed to contextListeners.


I didn't mean from the context listeners. I just said request.getContextPath() is a way to get the context.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by vishwanath nadimpally:


I didn't mean from the context listeners. I just said request.getContextPath() is a way to get the context.


Right, but the original poster was looking for a way to get it from a context listener:

That's fine in Servlets
But how can I get a Context in the ServletContextListener.
I want to read a file while the application starts.
-Prabhakar
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Originally posted by Ben Souther:
[QB][/QB]


my bad!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Prabhakar Rao
Ranch Hand

Joined: Aug 23, 2006
Posts: 40
Thanks all
I found the solution for this post.
Please suggest a solution for my other post regarding Response headers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Application Root