WEB application reads configuration files

  Abstract: WEB application reads configuration files 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="681" border="0"> <tr> <td width = " 397 "> 

  Suppose you Proxool.properties document attributes in the database configuration information: 
  Jdbc-0.proxool.alias = Access 
  Jdbc-0.proxool.driver-class = sun.jdbc.odbc.JdbcOdbcDriver 
  Jdbc-0.proxool.driver-url = jdbc: odbc: cwb 

  Jdbc-0.proxool.maximum-connection-count = 20 
  Jdbc-0.proxool.prototype-count = 4 

  Jdbc-0.proxool.house-keeping-test-sql = select CURRENT_DATE 
  Jdbc-0.proxool.verbose = true 
  Jdbc-0.proxool.statistics = 10s, 1m, 1d 
  Jdbc-0.proxool.statistics-log-level = ERROR 

  You attribute this document will be placed on your application's WEB web-info/classes directory, or in the bean category in the document to visit the traditional methods of document attributes Wuguo so: 

  </ Td> <td width="274"> 
  </ Td> </ tr> </ table> 

  Package examples; import java.io. *; import java.util .*; public class EnvironmentConfig (static EnvironmentConfig ec; / / Create Object ec private static Hashtable register = new Hashtable ();// static object initialized [in other Before object] private EnvironmentConfig () (super ();) / ** * EnvironmentConfig made an example of * @ return ec * / public static EnvironmentConfig getInstance () (if (ec == null) ec = new EnvironmentConfig (); / / create EnvironmentConfig target return ec; / / return EnvironmentConfig target) / ** * * @ read configuration files * @ param java.lang.String fileName return Properties * / public Properties getProperties (String fileName) (/ / transmission configuration file path InputStream is = null; / / definition of input streams is Properties p = null; try (p = (Properties) register.get (fileName); / / fileName stored in a HashTable / ** * If it is empty Try entered into documents * / if (p == null) (try (is = new FileInputStream (fileName); / / create input streams) catch (Exception e) (if (fileName.startsWith ("/")) / / use getResourceAsStream () method used to locate and open external documents.    EnvironmentConfig.class.getResourceAsStream is = (fileName); else is EnvironmentConfig.class.getResourceAsStream ("/"+ = fileName);) p = new Properties (); p.load (is); / / load the input flow register. put (fileName, p) / / stored in its cache is.close ();// HashTable closed input flow)) catch (Exception e) (e.printStackTrace (System.out);) return p; / / Properties returned to the object) / ** * insert description.    * Create Date: (2003-8-10 12:30:09) * @ param fileName java.lang.String * @ param strKey java.lang.String * / public String getPropertyValue (String fileName, String strKey) (Properties p = getProperties (fileName); try (return (String) p.getProperty (strKey);) catch (Exception e) (e.printStackTrace (System.out);) return null;)) 

  Below are the jsp test: testpro.jsp 

  <% @ Page contentType = "text / html; GBK charset ="%> 
  <% @ Page import = "examples.EnvironmentConfig"%> 
  <% @ Page import = "java.util .*"%> 

  Test configuration file 

<%
  EnvironmentConfig ec = EnvironmentConfig.getInstance (); 
  / / Properties p = ec.getProperties ( "/ Proxool.properties"); 
  String s = ec.getPropertyValue ( "/ Proxool.properties", "jdbc-0.proxool.driver-class"); 
  Out.println ( "jdbc-0.proxool.driver-class =" + s); 

%>

  The result: 

  Test configuration file 
  Jdbc-0.proxool.driver-class = sun.jdbc.odbc.JdbcOdbcDriver 

  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply