This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing a file under WEB-INF using getResource()
 
Similar Threads
Web-App Directory Structure!
Question Bank ID : 36
JSPs under the WEB-INF dir is OK
Internal Servlet Error ?
JSP hiding