• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can servlet use the file which is kept outside the classes folder.

 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

can a servlet access a file which is kept outside the classes folder.

if yes, how to do it.

i mean, i have a file in contextpath say "MY_WEB_APPLICATION/my_text_file" and i have servlet in MY_WEB_APPLICATION/WEB-INF/classes/x/y/z/AccessFileServlet.classes

Question:

how can i access the file in servlet.

Thanks In Advance.

Bennet Xavier. X

 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent all last night getting a FileNotFoundException when I tried reading a text file from the container (in the root folder). And, getServletContext().getRealPath("") is the only thing that worked.

 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan Beckett,

i have asked the same question here also.

if you are developing the application in windows and deploying in unix enviroment will that not make any problem.

do reply.

Bennet Xavier. X
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bennet Xavier wrote:

if you are developing the application in windows and deploying in unix enviroment will that not make any problem.

Bennet Xavier. X


Bennet,

Thats not the issue. getRealPath() would return actual path in the server , but reutrns null if deployed as war file

You can also use getServletContext().getResourceAsStream("/my_text_file") . remember / points to context root and path should be relative to Context root
 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

if file in context path say "MY_WEB_APPLICATION/my_text_file" and i have servlet in MY_WEB_APPLICATION/WEB-INF/classes/x/y/z/AccessFileServlet.classes

i tried to access the file using "../../../../../my_text_file"

if this is wrong, please tell me why.

Thanks,
Bennet Xavier. X
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bennet Xavier wrote:Hi,

if file in context path say "MY_WEB_APPLICATION/my_text_file" and i have servlet in MY_WEB_APPLICATION/WEB-INF/classes/x/y/z/AccessFileServlet.classes

i tried to access the file using "../../../../../my_text_file"

if this is wrong, please tell me why.

Thanks,
Bennet Xavier. X


Why do you want to access the file in such a way. As already mentioned , you get the context root and navigate from that , and not from the current servlet class path. I guess you understand what i mean by context root.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The path must begin with a "/" and is interpreted as relative to the current context root.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic