• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic