• Post Reply Bookmark Topic Watch Topic
  • New Topic

File acessing from an Servlet.

 
Claudio Gualberto
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I have an Servlet that is running on Tomcat 4.0 . This Servlet, over other things, acess files from within it's application context, this way :
File f = new File("../webapps/MyApplicationRoot/WEB-INF/conf/myFile.txt" );
This works well on Tomcat, but when i try deploy this application on JBoss 3.0, the method File.exists() returns false.
Then, i tried it :
File f = new File("../webapps/MyApplicationRoot/conf/myFile.txt" );
and it does not worked too.
I made no change on application's structure to deploy it on JBoss, the only difference is that in Tomcat the application is not JARed ( i did not packaged it to a .WAR file ) and in JBoss i did.
Could it be the reason, or i should write another descriptor ( .XML ) in the .WAR file to make it works on JBoss?
Thanks in advance.
Claudio Gualberto.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That "../" notation depends on the "current directory" of the servlet JVM so it is NOT something universal. You have two options if you want to create a File:
1. pass an absolute directory location in web.xml
2. use the getRealPath method in the servlet context to convert from the relative location (WEB-INF/conf/myText.txt) to an absolute location.
Bill
 
Claudio Gualberto
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping,
That method from servlet context worked well, but only when my application is not in a WAR file,when i JAR it to an WAR file this method returns null. What should i do in this case ?
Claudio Gualberto.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never tried to do that so I don't have any bright ideas for you!
 
Mark Bensing
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at the getResource and getResourceAsStream methods of java.lang.Class. These methods can access a resource that is in the classpath; I assume you probably just want to read the file - getResourceAsStream() will return an InputStream you can use to read the file. I have never tried to access a file from a WAR file as you are trying to do, but I think it should work.
I hope this helps,
Mark
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!