File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Accessing a file under WEB-INF using getResource() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Accessing a file under WEB-INF using getResource()" Watch "Accessing a file under WEB-INF using getResource()" New topic
Author

Accessing a file under WEB-INF using getResource()

chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
I have a file under WEB-INF folder named a.txt

In may Servlet code MyServlet.java i use:

getServletContext().getResource("/WEB-INF/a.txt");

it evaluates to null.

But as SRV.9.5 notes:


The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEBINF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext, and may be exposed using the RequestDispatcher calls. Hence, if the Application Developer needs access, from servlet code, to application specific configuration information that he does not wish to be exposed directly to theWeb client, he may place it under this directory.

Why what is the problem???


Thanks,
Chandan Kumar
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
HI Chandan

I am able to get the value as




My servlet code:



So just check if you have placed that file directly under WEB-INF or in WEB-INF/classes.Also make sure that a.txt is deployed properly to the server.
chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
Hi Raj,

Will you please send me the war file
chandan kumar mitwaa
Ranch Hand

Joined: Jul 01, 2009
Posts: 49
Thanks Raj,
I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a file under WEB-INF using getResource()