Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

context root

 
Ishita Saha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your servlet, you can use HttpServletRequest#getContextPath
 
Ishita Saha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply,

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

Thanks!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ishita Saha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ishita Saha
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do not you pass the location as a servlet config parameter?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic