0

I have a JSP/Servlet project and I would like to access a .json or a .xml file from one of my servlets. But where do I place those files?

enter image description here

I can access

  String filename = "/WEB-INF/myFile.xml";

But how about the following:

    filename ="/WEB-INF/resources/myFile.xml";

? How do I get the file from /resources?

  ServletContext context = getServletContext();
  InputStream is = context.getResourceAsStream(filename);

Where do I save files, that I want deployed and accessable from my servlets with my application to tomcat/webapps?

Gero
  • 10,601
  • 18
  • 58
  • 101
  • 1
    You didn't place it in `/WEB-INF/resources/myFile.xml`. You placed it in `/resources/myFile.xml`. – BalusC Apr 30 '15 at 20:21

1 Answers1

0

Put them in the classpath (where your java classes are placed). If you want to put them in the separate folder (resources for e.g.) create a resources package and store files there, so then you will be able to access and read them using:

if files stored in the classpath root:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("myFile.xml");

if files are stored in the resources package:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/myFile.xml");

Just note that / separator is OS specific so it's better to get it using System.getProperty("file.separator"); and don't worry about the OS.

Paulius Matulionis
  • 21,481
  • 21
  • 97
  • 139