2

I am developing a jsp + servlet application, package to war, deploy to tomcat 8.

I know WEB-INF is the folder to store files which are not to be served, only processed at server side: like html or jsp fragments, etc. I need to store some xmls. Servlets would read those xmls when processing requests.

The question is:

should I store these xmls in the same WEB-INF folder? Or is there a special place for that? I don't want to do it very anti-pattern.

Androrider
  • 950
  • 2
  • 10
  • 19

2 Answers2

4

If you are using maven, you could put your XML files in src/main/resources. When packaged as a WAR, they will be placed in the WAR's WEB-INF/classes folder, e.g.

src/main/resources/settings.xml

Will get packaged to:

WEB-INF/classes/settings.xml

That way you can read the file using e.g.:

MyServlet.class.getResourceAsStream("/settings.xml");

Once deployed to tomcat and it gets unpacked, you can then edit / update the settings.xml in the WEB-INF/classes folder if you need to. Note that the /WEB-INF/classes folder is the root of the classpath.

Willy du Preez
  • 923
  • 8
  • 10
0

Put it in WEB-INF/, and use the getResourceAsStream() method on the ServletContext object, e.g.

servletContext.getResourceAsStream("/WEB-INF/myfile");

How you get a reference to the ServletContext depends on your application... do you want to do it from a Servlet or from a JSP?

See here: Howto load a resource from WEB-INF directory of a web archive

Community
  • 1
  • 1
mikeb
  • 8,943
  • 4
  • 43
  • 94