This example uses the log4j log component to output the exception information of the servlet-type database connection to the console and log4j log files when the Oracle database is not started.
1. Web. xml file configuration
<? XML version = "1.0" encoding = "gb2312"?> <Br/> <web-app xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" <br/> Version = "2.4"> <br/> <servlet-Name> dbtestlog4jservlet </servlet-Name> <br/> <servlet-class> COM. test. log4j. dbtestlog4jservlet </servlet-class> <br/> <init-param> <br/> <param-Name> log4j-init-file </param-Name> <br/> <param-Value> WEB-INF/log4j. properties </param-value> <br/> </init-param> <br/> </servlet> <br/> <servlet-mapping> <br/> <Servlet -Name> dbtestlog4jservlet </servlet-Name> <br/> <URL-pattern>/dbtestlog4jexcep </url-pattern> <br/> </servlet-mapping> <br/> </Web-app>
2. Compile the servlet class with the following code:
Package COM. test. log4j; </P> <p> Import Java. io. *; <br/> Import Java. SQL. *; <br/> Import javax. servlet. *; <br/> Import javax. servlet. HTTP. *; <br/> Import Org. apache. log4j. *; <br/> public class dbtestlog4jservlet extends httpservlet <br/>{< br/> static logger = logger. getrootlogger (); <br/> static logger booklogger = logger. getlogger ("bookstorelogger"); </P> <p> Public void Init () throws servletexception <B R/>{< br/> string prefix = getservletcontext (). getrealpath ("/"); <br/> string file = getinitparameter ("log4j-init-file"); <br/> If (file! = NULL) <br/>{< br/> propertyconfigurator. configureandwatch (prefix + file); <br/>}< br/> try <br/>{< br/> class. forname ("oracle. JDBC. driver. oracledriver "). newinstance (); </P> <p >}< br/> catch (classnotfoundexception CE) <br/>{< br/> throw new unavailableexception ("failed to load the database driver! "); <Br/>}< br/> catch (exception e) <br/>{< br/> E. printstacktrace (); <br/>}</P> <p> Public void doget (httpservletrequest req, httpservletresponse resp) <br/> throws servletexception, ioexception <br/>{< br/> connection conn = NULL; <br/> statement stmt = NULL; <br/> try <br/> {<br/> conn = drivermanager. getconnection (<br/> "JDBC: oracle: thin: @ localhost: 1521: Test", "ctcmc", "ctcmc"); <br/> stmt = Conn. createstatement (); <br/> resultset rs = stmt.exe cutequery ("select * From rp_country_min5"); <br/>}< br/> catch (sqlexception SE) <br/> {<br/> logger. warn ("logger. warn database operation failed! "+ Se); <br/> logger. Error (" logger. Error database operation failed! "+ Se); </P> <p> booklogger. Warn (" booklogger. Warn database operation failed! "+ Se); <br/> booklogger. Error (" booklogger. Error database operation failed! "+ Se); <br/> resp. senderror (httpservletresponse. SC _internal_server_error, <br/>" A database operation error occurs. Contact the administrator. "); <Br/>}< br/> finally <br/>{< br/> If (stmt! = NULL) <br/>{< br/> try <br/>{< br/> stmt. close (); <br/>}< br/> catch (sqlexception SE) <br/>{< br/> booklogger. error ("An error occurred while disabling statement! ", SE); <br/>}< br/> stmt = NULL; <br/>}< br/> If (Conn! = NULL) <br/>{< br/> try <br/>{< br/> Conn. close (); <br/>}< br/> catch (sqlexception SE) <br/>{< br/> booklogger. error ("An error occurred while closing the database connection! ", SE); <br/>}< br/> conn = NULL; <br/>}< br/>}
3. Edit the log4j. properties configuration file.# The Log Level of the root recorder is error. Logs below this level are ignored. <Br/> # specify the console appender for the root recorder <br/> log4j. rootlogger = error, console </P> <p> # defines the bookstorelogger recorder. If no log level is set, it inherits the root logger level. <Br/> # specify the appender named file for the bookstorelogger recorder. The bookstorelogger Recorder also inherits the appender of the root record. <Br/> log4j. Logger. bookstorelogger =, file </P> <p> # define an appender named console, whose type is leleappender. <Br/> log4j. appender. Console = org. Apache. log4j. leleappender <br/> # the console appender uses simplelayout. <Br/> log4j. appender. console. layout = org. apache. log4j. simplelayout </P> <p> # defines an appender named file. Its type is fileappender. <Br/> log4j. appender. File = org. Apache. log4j. fileappender <br/> # specify the name and storage path of the log file output by file appender. <Br/> log4j. appender. file. file = D: // web/webserver/WEB-INF/log4j. log </P> <p> # The layout file used by file appender is patternlayout. <Br/> log4j. appender. file. layout = org. apache. log4j. patternlayout <br/> # specify the log output format <br/> log4j. appender. file. layout. conversionpattern = % d {yyyy-mm-dd hh/: mm/: SS} [% C]-[%-5 p] % m % N
4. deploy the preceding web application, start Tomcat, and enter http: // localhost: 8080/CTC/dbtestlog4jexcep in the address bar. The log information will be displayed on the console and D: /web/webserver/WEB-INF/log4j. log File output
The console output is as follows:Error-logger. Error database operation failed! Java. SQL. sqlexception: Listener refused the connection with the following error: <br/> ORA-12505, TNS: listener does not currently know of SID given in connect descriptor <br/> the connection descriptor used by the client was: <br/> localhost: 1521: test </P> <p> error-booklogger. error database operation failed! Java. SQL. sqlexception: Listener refused the connection with the following error: <br/> ORA-12505, TNS: listener does not currently know of SID given in connect descriptor <br/> the connection descriptor used by the client was: <br/> localhost: 1521: Test
The output of the log4j. log file is as follows:20:27:22 [bookstorelogger]-[Error] booklogger. Error database operation failed! Java. SQL. sqlexception: Listener refused the connection with the following error: <br/> ORA-12505, TNS: listener does not currently know of SID given in connect descriptor <br/> the connection descriptor used by the client was: <br/> localhost: 1521: Test <br/>
Now, log4j log configuration is complete!