Description:
Mainly use JNDI to call ENV and SOURCE in Tomcat
problem:
Note that the JDBC driver needs to be placed in $ {catalina_home} / common / lib directory
Pay attention to the order of labels in Tomcat configuration files
# Server.xml configuration is as follows
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
parameter>
Resourceparams>
Context>
#Web.xml configuration
#JSP test uses the following
<% @ Page ContentType = "Text / HTML; Charset = GB2312" Import = "javax.naming. *, java.sql. *, javax.sql. *"%> <% // Build a Context as findings here Myenv CTX is initial source CTEXT CTX = new initialContext (); // Note that the configured lookup in Tomcat is in Java: Comp / ENV below CTX.lookup ("Java: Comp / ENV"); / / Here you get a Server-level environment variable integer Greetings = (Integer); "Greetings"); Out.println ("Serverenv IS:" Greetings "); // Heb here is web Level environment variable Out.println ("WebENV IS:" (String) MyEnv.lookup ("Webenv") ""); / * Using the DataSource configured in Tomcat to perform database operation * / connection conn = NULL; DATASOURCE DS = NULL; Statement Stat = NULL; ResultSet Res = NULL; Try {//class.forname ("Net.SourceForge.jtds.jdbc.driver "); // Get the Tomcat configuration DataSource refers to DS = ( DataSource) Myenv.lookup ("JDBC / DS"); // Gets Connection if (DS! = Null) {conn = ds! = Null ("Had Data Source ...");} Stat = conn.createStatement (); // Execute a statement and returns a resultset res = stat.executeQuery ("Select Distinct Top 20 * from Orders); / / Print ResultSet Out.print ("<"); while (res. proper ()) {out.print (res. maxT (1) ":" res. maxTString (2)
");}}}}} {Out.println (e);} finally {if (res! = Null) res.close (); if (stat! = Null) stat.close (); if Conn! = null) Conn.close ();} // Close Context is said that context is best to shut down ctx.close (); myenv.close ();%> # display results as follows
Serverenv is: 15
Webenv IS: I am Environment in Web.xml ...
conn
10248: Vinet
10249: Tomsp
10250: Hanar
10251: Victe
10252: SUprd10253: Hanar
10254: ChOPS
10255: RICSU
10256: Welli
10257: Hilaa
10258: Ernsh
10259: CENTC
10260: Ottik
10261: Quede
10262: Rattc
10263: Ernsh
10264: Folko
10265: Blonp
10266: Warth
10267: Frank