0

I've a web application and and XML file in the WEB-INF directory. I need to load this xml file. Is there other ways of loading it rather than using getServletContext().getResourceAsStream("/WEB-INF/test.xml");

I tried options like Thread.currentThread().getContextClassLoader().getResourceAsStream("/WEB-INF/test.xml");

It is not working.

Could you please help me?

  • Q: What exactly is wrong with the "getServletContext()" method? – paulsm4 Sep 17 '12 at 19:59
  • I want to load this from a static block. –  Sep 17 '12 at 20:02
  • duplicate [Howto load a resource from WEB-INF directory of a web archive](http://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a-web-archive) – user1406062 Sep 23 '12 at 12:32

1 Answers1

1

you can access resources from within WEB-INF folder just using /WEB-INF/test.xml from within your application (jsp - servlet).

<!%@ taglib uri="/WEB-INF/tiles-jsp.tld" prefix="tiles" %>

If you want to make it available to a web user then it CANNOT be accessed directly. For a user to access it directly, there must be some sort of interface that will expose the file from the WEB-INF folder (ex. jsp that reads the file /WEB-INF/test.xml and output's it to the jsp/servlet)

UPDATE

To read a file using a Servlet use this:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    StringBuffer strContent = new StringBuffer("");
    int ch;
    PrintWriter out = response.getWriter();
    try {
        String path = getServletContext().getRealPath("/WEB-INF/newfile.xml");
        File file = new File(path);
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
            while ((ch = fin.read()) != -1) {
                strContent.append((char) ch);
            }
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println("File " + file.getAbsolutePath()
                    + " could not found");
        } catch (IOException ioe) {
            System.out.println("Exception while reading the file" + ioe);
        }
        System.out.println("File contents :");
        System.out.println(strContent);
    } finally {
        out.close();
    }
}

This will read a file named newfile.xml from WEB-INF and will output its contents to the console. The output will be something like:

File contents :

< a >

< b >xxxx< /b >

< /a >

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • I need to do this from a static block in one of my class. I will not be allowed to call getServletContext() from there. –  Sep 18 '12 at 13:58
  • if you have access on a response object then you can get it like this ServletContext sc = request.getSession().getServletContext().getRealPath("/WEB-INF/newfile.xml"); If not you can pass response as parameter to your static block – MaVRoSCy Sep 18 '12 at 14:22
  • How do I pass a parameter to static block? static{//some code} –  Sep 19 '12 at 19:51