I have a bunch of text, xml and other files (i.e. resources) that I need to access using servlets in java web app. For example, there is an xml file, a part of which is returned with a servlet by a user query. I am using Tomcat. What is the best practice to store these files and access them from java code? 1) What are the default folders where should I put them, do I need to put them into Web archive or into one of the Jars? 2) How to access the files from java code? How can I set the path to them so it will work in any environment? P.S. I've read a number of posts related to this topic, most of which recommend to store resources in jars and access them using java.lang.Class.getResourceAsStream(String). It seems strange because classes and data should be separated.
相关问题
- What is the best way to do a search in a large fil
- Spring Integration - Inbound file endpoint. How to
- php--glob for searching directories and .jpg only
- Can you set the Location header in a chunked http
- Editing how file name is displayed in JComboBox wh
相关文章
- What is the correct way to declare and use a FILE
- java.lang.NoClassDefFoundError: javax/servlet/http
- Forward request from servlet to jsp
- Making new files automatically executable?
- Intercept @RequestHeader exception for missing hea
- File Upload of more than 4GB
- How to serialize data into indented json [duplicat
- Creating a custom file like object python suggesti
It's perfectly fine to load static resources using the classloader. That's what ResourceBundle does to load the internationalized properties files for example.
Put them in
WEB-INF/classes
along with your class files, or in a jar insideWEB-INF/lib
, and load them with the ClassLoader as indicated by the answers you already read.That doesn't forbid you to place these files in a separate directory from the Java source files in your project. The build process should just make sure to put them in the appropriate location for runtime. The Maven and Gradle convention is to put the source files under
src/main/java
and the resource files undersrc/main/resources
.