Building java web development environment and Application


One uses Tomcat server and DBCP data source to build web development environment
1. JDK installation, the default path is OK
2. Tomcat5.5 server
1) Configure the server port of Tomcat:
Open D: apache-tomcat-5.0.18, conf.server.xml to see the following code:
<!– Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 –>
<Connector port=”8080″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”false” redirectPort=”8443″ acceptCount=”100″
debug=”0″ connectionTimeout=”20000″
disableUploadTimeout=”true” />
Port = 8080 is the port where Tomcat provides Web services
2) Enter the console
You must enter a user name and password to log in to the manager console
First, open D: (apache-tomcat-5.0.18) (webapps) (Manager) (WEB-INF) (web.xml), and check the following code:
<!– Define the Login Configuration for this Application –>
<!– Determine the login mode of JAAS — >
<realm-name>Tomcat Manager Application</realm-name>
<!– Security roles referenced by this web application –>
<!– Determine the security role required to log in to the application — >
The role that is required to log in to the Manager Application
<!– Only the manager role can log in to the application — >
Secondly, open d://apache-tomcat-6.0.18/conf/tomcat-users.xml to configure the user name, password and role of Tomcat. The code is as follows:
<?xml version=’1.0′ encoding=’utf-8′?>
<!– Configure Tomcat user, password, role — >
<!– Configure Tomcat role — >
<role rolename=”tomcat”/>
<role rolename=”role1″/>
<role rolename=”manager”/>
<role rolename=”admin”/>
<!– Configure Tomcat users — >
<!– Configure the first user with the user name of tomcat, password of tomact and role of Tomcat — >
<user name=”tomcat” password=”tomcat” roles=”tomcat” />
<!– Configure the second user, with the user name of role1, password of tomact, and role of role1 — >
<user name=”role1″ password=”tomcat” roles=”role1″ />
<!– Configure the third user, whose user name is both, password is tomact, and roles are Tomcat and role1 — >
<user name=”both” password=”tomcat” roles=”tomcat,role1″ />
<!– Configure the user who logs in to the manager console. The user name is manager, the password is manager, and the role is manager — >
<user name=”manager” password=”manager” roles=”manager” />
3) Deploy web application
The main ways for Tomcat to deploy web applications are as follows:
Using console deployment; The automatic deployment function of Tomcat is used to deploy; The web application is deployed by modifying the server. XML file; Add custom web deployment file
Using console deployment and modifying server.xml file deployment are essentially the same. We should try to avoid modifying server.xml file, so these two methods are not recommended
Automatic deployment is to copy web application to webapps path of tomcat, and Tomcat will load the web application automatically
Add custom web deployment file:
Enter the path D: tomcat-5.0.28, conf, Catalina and localhost. There are three configuration files by default in this path. Copy one of the three files,
And rename the file. It is recommended that the file name should have the same name as the deployed web application
<!– Deploy a web application, where path is the virtual path of the web application and docbase is the document path of the web application — >
<Context path=”/test” docBase=”e:/webroot” debug=”0″ privileged=”true”>
4) Configure the data source of Tomcat
Tomcat itself does not have the ability to provide data sources. With the help of some other open source data sources, such as DBCP, c3p0 and so on, Tomcat can provide data sources
Through the data source provided by tomcat, the program can find the data source through JNDI, providing more convenient persistent layer access
Data source configuration: global data source, local data source
The jar files of DBCP data source (commons-dbcp-1.2.1. Jar, commons-pool-1.2. Jar, commons-collections-3.1. Jar) and database driver are needed
Local data source configuration: after modifying the file D: / / tomcat-5.0.28 \ \ conf \ \ Catalina \ \ localhost \ \ test.xml and adding a local data source:
<?xml version=’1.0′ encoding=’utf-8′?>
<!– Configure a web application — >
<Context path=”/test” docBase=”e:/webroot” debug=”0″ privileged=”true”>
<!– Configure a resource with the name of JDBC / dstest and the type of data source — >
<Resource name=”jdbc/dstest” auth=”Container” type=”javax.sql.DataSource” />
<!– Define the parameters of the resource, and the name property specifies which resource parameter to define — >
<ResouceParams name=”jdbc/dstest”>
<!– The following defines the parameters of the data source — >
<!– Define data source factory — >
<!– Defines the maximum number of active connections for a data source — >
<!– Define the timeout time of the data source. After this time, the data source will be disconnected automatically
<!– Define the maximum number of idle connections in the data source. Once the number of idle connections in the container exceeds this number, the system will automatically destroy some connections — >
<!– Define the maximum number of waits for the data source — >
<!– User name to connect to database — >
<!– Password to connect to database — >
<!– The driver used to connect to the database — >
<!– URL to connect to database — >
Start Tomcat and use JNDI to access the data source. The reference code is as follows:
//Initialize context, use initialcontext to initialize context
Context ctx = new InitialContext();
*The JNDI is Java: comp / env / JDBC / dstest, which is divided into two parts,
*Java: comp / env is fixed by Tomcat. The JNDI binding provided by Tomcat must be prefixed
*JDBC / dstest is the name of the data source when the data source is defined
DataSource ds = (DataSource) ctx.looup(“java:comp/env/jdbc/dstest”);
//Get database connection
Connection conn = ds.getConnection();
//Get statement
Statement stmt = conn.createStatement();
//Execute the query and return the resultset object
ResultSet rs = stmt.executeQuery(“select * from user”);
while ( {

Configuration of all data sources: modify sever.xml file to refer to the configuration of local data sources
3. Eclipse environment
Direct installation of plug-ins: it means to copy the contents of the plugins and features folder contained in the plug-in to the plugins and features folder of eclipse, and restart eclipse
Extension installation plug in:
1) Create a new links folder in the eclipse installation path
2) In the links folder, create a file. The name of the file is arbitrary, but the suffix must be link. It is generally recommended to make the name of the file the same as the plug-in name
3) Edit, which usually only needs one line:
path = sync
Where path = is fixed and sync is the folder name
4) Create a new eclipse folder under the path indicated by path in file, and then create plugins and features folder in eclipse folder
5) Copy the contents of the plugins and features folder contained in the plug-in to the plugins and features folder created above, and then restart eclipse
DBCP is one of the most commonly used java open source connection pools. It is generally used in systems where databases are frequently used. It can handle a large number of database connection requests. It is the preferred database configuration for large sites
Second, the creation process of DBCP data source
1. Create data source: create data source object DS through basic datasource class of DBCP, set data source driver class, user name, password and connection URL
2. Close data source: close the created data source object
3. Test data source: test the use of data source in the main() function. First, get the data source DS, call the getconnection function of the data source to get the connection object conn of the database, and then the operation is exactly the same as the common operation of JDBC
The test classes are as follows:
*Test the connection application of DBCP
package com.test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
* @author johnston678
* @version 2009-04-27
public class DataSourceExample {
//Create data source
public static DataSource setupDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
return ds;
//Close data source
public static void shutdownDataSource(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
public static void main(String[] args) {
//Create basicdatasource
DataSource dataSource = setupDataSource(“jdbc:oracle:thin:@”);
//Creating JDBC objects
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
st = conn.createStatement();
String sql = “select username from loginuser”;
rs = st.executeQuery(sql);
int numcols = rs.getMetaData().getColumnCount();
while ( {
for (int i=1; i<=numcols; i++) {
} catch (SQLException e) {
} finally {
try {
} catch (SQLException e) {
Using DBCP connection pool in Tomcat
Tomcat uses DBCP database connection pool by default. The jar file required by Tomcat 6.0 is tomcat-dbcp.jar
1. Defined in context.xml or defaultcontext.xml in Tomcat
<!– Default set of monitored resources –>
<!– Uncomment this to disable session persistence across Tomcat restarts –>
<Manager pathname=”” />
<!– Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) –>
<Valve className=”org.apache.catalina.valves.CometConnectionManagerValve” />
<!– Data source configured with DBCP — >
<Resource name=”jdbc/struts_dbcp_connect”
maxWait=”10000″ />
2) In web.xml, configure the < resource ref > element to reference JNDI resources in web applications
<description>struts dbcp connect</description>
3) Using data source in Web Application
Get a reference to the data source:
Context ctx = new InitalContext();
DataSource ds = (DataSource) ctx.lookup(“java:comp/env/jdbc/struts_dbcp_connect”);
Get database connection object:
Connection conn = ds.getConnection();
Return database connection to connection pool:
Fourth, configure DBCP connection pool in struts
DBCP is also the default connection pool of struts. Follow the steps below to configure the connection pool of DBCP in struts
1. Deploy DBCP jar package and Oracle driver package
The jar file of DBCP in Tomcat 6.0 is tomcat-dbcp.jar
The jar file of Oracle’s driver package is: C:: (Oracle) product 10.2.0 dB_ 1\jdbc\lib\classes12.jar
2. Configure DBCP data source in struts-config.xml as follows:
<!– Configuring DBCP data source in Struts
<data-source type=”org.apache.tomcat.dbcp.dbcp.BasicDataSource”>
<set-property property=”driverClassName” value=”oracle.jdbc.driver.OracleDriver”/>
<set-property property=”url” value=”jdbc:oracle:thin:@”/>
<set-property property=”username” value=”strutsdb”/>
<set-property property=”password” value=”strutsdb”/>
<set-property property=”maxActive” value=”10″/>
<set-property property=”maxWait” value=”5000″/>
<set-property property=”defaultAutoCommit” value=”false”/>
<set-property property=”defaultReadOnly” value=”false”/>
<set-property property=”validationQuery” value=”SELECT COUNT(*) FROM loginuser”/>