This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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:
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?
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.
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.
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
Joined: Nov 29, 2005
Why do not you pass the location as a servlet config parameter?