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 context root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "context root" Watch "context root" New topic
Author

context root

Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
Hi,

I'm working on an intranet site project.I've to write a servlet to store some temp files on the machine.
For that i need to get the temp folder location at run time. I want to use relative path for that.

So lets say for my sandbox setup, the application is deployed on tomcat in the location:

C:/projects/server/apache-tomcat-5.5.23/webapps/fstkservices/

and i want to save the temp files in a folder tempupload under fstkservices. So my question is that how do i get the above location in my servlet code. Is there any API to get the root location of where the application is deployed?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

From your servlet, you can use HttpServletRequest#getContextPath


[My Blog]
All roads lead to JavaRanch
Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
thanks for your reply,

I used getServletContext().getRealPath("."); which gives me the said location of my deployed application.

Thanks!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Ishita Saha:
Is there any API to get the root location of where the application is deployed?


It depends on how your application has been deployed.
Java Web Apps can be deployed either as expanded file systems or as packed war files. Depending on your container and it's settings, war files may or may not be unpacked when they are deployed.

If your app is running from an expanded file system, you can use ServletContext.getRealPath("/") to find the root of your web application.
If it's running from a packed war file the getRealPath method will return null because there really isn't a path to your application.


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

Joined: May 30, 2007
Posts: 39
Thanks for the heads up Ben,

For my sandbox it's running fine, i'll check how it's there in the production.
Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
Hi Ben,

So what should be the safe solution for the said problem. If you can help me out.

I mean say i don't know that my war would be expanded or not - what should i use in that case which works for whatever the case may be.

Thanks.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Why do not you go for a configuration file for the entire application.With one of the properties as the temp file location.Then in the application you are read that to a java properties object and query that for various values.

If you put the file in WB-INF/conf directory the use servletContext.getResourceAsStream("/WB-INF/conf/app.properties"); to get the inputstream .Later feed that into the properties object.


Rahul Bhattacharjee
LinkedIn - Blog
Ishita Saha
Ranch Hand

Joined: May 30, 2007
Posts: 39
well - i'm not sure that my client would like the idea of a configuration file just for this singlr purpose.

the complete issue is:
I've to look for that if a particular folder named App exists in my application package and if not i've to create this folder and then later in the code i've to save some temp files in that folder
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Why do not you pass the location as a servlet config parameter?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: context root
 
Similar Threads
Image upload problem when using Tomcat 6 and org.apache.commons.fileupload
Tomcat6 and Java Beans
writing to Temp files
servlet API
temp location