Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

java - Reading Web Application Resources

Background

Developing a simple web application (Eclipse + JBoss + Apache Tomcat) to generate XML files.

Problem

The "Business Area" list queries against the database, and the "Column Cluster" list queries the database using the selected "Business Area" items. Both of these are unique queries that are stored external text files.

The files are currently stored in the following locations:

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

These are then used to seed PreparedStatements.

Source Code

The method to read the SQL code might 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 would like to know:

  1. What are the best practices for storing such assets for deployment as part of a web app? (That is, is META-INF a good location, or is META-INF/resources preferred?)
  2. What APIs would you recommend for reading the file content? (That is, how do I write the open method so that it finds the files to open?)

I already have JNDI in place to establish the database connection, but would rather not use JNDI to obtain handles to the files, if possible.

Related Sites

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The right location (and also the common practice) is to place them under your source directory, which will then gets compiled into WEB-INF/classes directory. I'm not sure what you meant by "classes directory is volatile" in your response to @Dave, but this is how most (if not all) Java web apps store things. WEB-INF/classes is not just for Java classes. It's common to see logging properties file (like log4j), Hibernate and Spring XML files stored under 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 about the use of META-INF: What's the purpose of META-INF?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...