JSP -> Web Application Properties

xiaoxiao2021-03-06  40

March 1, 2005

In web applications, some database connection parameters or parameters of the system itself are usually not written in the program, and need to be saved in the form of an attribute file or the form of an XML file. Each of them has advantages and disadvantages, the formal operations and management of the attribute file is relatively simple, and the XML file form provides very powerful and hierarchical properties. The following is explained that some important parameters are recorded in the way in Web applications. The following example uses a database connection parameter as an example, the development environment is Eclipse, and the deployment environment is Tomcat. The content file is as follows: Init.properties ------------------------------------------------------------------------ -------------------- DRIVERS = Oracle.jdbc.driver.OracleDriver URL = JDBC: Oracle: Thin: @ 127.0.0.1: 1521: Test UserName = Testuser Password = TEST ------------------------------------------------------------------------------------------------------------------------------------------------------ ------------- Access to this property file: InitpropertiesLoader.java --------------------------- ---------------------------------------- package com.knight.commonspublic class initpropertiesLoader {/ ** * direct read attribute file in the classpath * @param filename and path of the file name * @return * / public properties getProperties (String filename) {properties prop = new properties (); InputStream in = null; try {in = getClass () .GetResourceceAsStream (filename); Prop.Load (in);} catch (exception e) {log.info ("Unable to read database connection configuration properties file!");} finally {try {if (in! = NULL) {in.close (); }}}}} Return Prop;} -------------------------------------- -------------------------------

ConnectionManager.java ----------------------------------- -------------------- package com.knight.commons.databasepublic class ConnectionManager {private static String driverName = null; private static String url = null; private static String username = null; private static String password = null; private static Properties prop = null; public void init () {try {prop = new InitPropertiesLoader () getProperties ( "/ init.properties");. driverName = prop.getProperty ( "drivers" ); Url = prop.getProperty ("URL"); username = prop.getProperty ("username"); password = prop.getProperty ("password");} catch (Exception E) {log.info ("Read Properties An error occurred when configuring the file! ");} Finally {try {if (prop! = Null) prop.clear ();} catch (e.Getmes) {log.info Sage ());

转载请注明原文地址:https://www.9cbs.com/read-52229.html

New Post(0)