Reading web application resources

Background

Development of a simple web application (Eclipse + JBoss + Apache Tomcat) for creating XML files.

Problem

A database is requested in the "Business area" list, and a database is requested in the "Column column" list using the selected "Business zone" elements. Both of these are unique queries that store external text files.

Currently, files are stored in the following places:

  • WebContent / META-INF / business areas.sql
  • WebContent / META-INF / Columned clusters.sql

They are then used to sow PreparedStatement s.

Source

The method for reading SQL code may resemble:

  private String getSQL() { String result = ""; try { BufferedReader br = open( "business-areas.sql" ); String line = null; while( (line = br.readLine()) != null ) { result += line; } br.close(); } catch( Exception e ) { e.printStackTrace(); } return result; } 

Questions

I'd like to know:

  • What are the best practices for storing such assets for deployment as part of a web application? (That is, META-INF good place or preferably META-INF/resources ?)
  • What APIs would you recommend for reading the contents of a file? (That is, how to write the open method so that it detects that the files open?)

I already have JNDI to establish a database connection, but rather do not use JNDI to get file descriptors, if possible.

Related Sites

Thanks!

+5
java web-applications servlets jsf jboss
source share
3 answers

The correct location (as well as common practice) is to put them in the source directory, which will then be compiled into the WEB-INF/classes . I'm not sure if you meant that the “class directory is volatile” in your answer to @Dave, but this is how most (if not all) Java web applications store things. WEB-INF/classes is not just for Java classes. It is common knowledge that log file files (e.g. log4j), Hibernate, and Spring are stored in the source directory, and you can safely access the files using something like this: -

 // in this case, the business-areas.sql is located right under "source/sql" directory InputStream is = getClass().getClassLoader().getResourceAsStream("sql/business-areas.sql"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); 

Some useful information on using META-INF: What is the purpose of META-INF?

+5
source share

I had the same problems as Dave Jarvis about mixing resources with classes and lib , so I played around a bit and found this solution:

I have placed resource files in WEB-INF/resources . Then, to download them, I used this:

 getClass().getClassLoader().getResourceAsStream("../resources/main.xml"); 

I don't know that using .. is a much cleaner solution, but my files at least don't mix with classes or banks.

+3
source share

I would put them in WEB-INF / classes or bundle them inside your application.jar, which will go into WEB-INF / lib. Then you can load them from the classpath, as described here and here.

Even better, if you are using maven, it is best to put these file types in src / main / resources, and then maven will take care of this for you.

0
source share

All Articles