Build a java WEB development environment and Application

Source: Internet
Author: User

1. Use the Tomcat server and DBCP data sources to build a Web development environment
1. JDK installation. The default path is enough.
2. Tomcat5.5 Server
1) configure the Tomcat server port:
Open D: \ apache-tomcat-5.0.18 \ conf \ server. xml and view the following code:
<! -- Define a non-SSL Coyote HTTP/1.1 Connector on port 8080 -->
<Connection 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 for Tomcat to provide Web services.
2) Go to the console
You must enter the user name and password to log on to the manager Console.
First, open D: \ apache-tomcat-5.0.18 \ webapps \ manager \ WEB-INF \ web. xml and view the following code:
<! -- Define the Login Configuration for this Application -->
<! -- Determine the JAAS logon Method -->
<Login-config>
<Auth-method> BASIC </auth-method>
<Realm-name> Tomcat Manager Application </realm-name>
</Login-config>
<! -- Security roles referenced by this web application -->
<! -- Determine the security role required to log on to the Application -->
<Security-role>
<Description>
The role that is required to log in to the Manager Application
</Description>
<! -- Only the manager role can log on to the application. -->
<Role-name> manager </role-name>
</Security-role>
Next, open D: \ apache-tomcat-6.0.18 \ conf \ tomcat-users.xml, configure the Tomcat user name and password and role, view the Code as follows:
<? Xml version = '1. 0' encoding = 'utf-8'?>
<! -- Configure the Tomcat user, password, and role -->
<Tomcat-users>
<! -- Configure the Tomcat role -->
<Role rolename = "tomcat"/>
<Role rolename = "role1"/>
<Role rolename = "manager"/>
<Role rolename = "admin"/>
<! -- Configure Tomcat users -->
<! -- Configure 1st users, with the username tomcat, password tomact, and role tomcat -->
<User name = "tomcat" password = "tomcat" roles = "tomcat"/>
<! -- Configure 2nd users, set username to role1, password to tomact, and role to role1 -->
<User name = "role1" password = "tomcat" roles = "role1"/>
<! -- Configure 3rd users, the user name is both, the password is tomact, And the role is tomcat and role1 -->
<User name = "both" password = "tomcat" roles = "tomcat, role1"/>
<! -- Configure the user logging on 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"/>
</Tomcat-users>
3) deploy Web Applications
The following describes how to deploy a Web application using Tomcat:
Deploy on the console; deploy using the automatic deployment function of Tomcat; deploy Web applications by modifying the server. xml file; and add custom Web deployment files
Deploying and modifying the server. xml file on the console is essentially the same. You should try to avoid modifying the server. xml file. Therefore, these two methods are not recommended.
Automatic deployment is to copy the Web application to the webapps path of Tomcat, and Tomcat will automatically load the Web application.
Add a custom Web deployment file:
Go to the D: tomcat-5.0.28 \ conf \ Catalina \ localhost path. By default, there are three configuration files, copy one of the three files,
Rename the file. We recommend that you name the file with the same name as the deployed Web application. For more information, see:
<! -- Deploy a Web application. path indicates the virtual path of the Web application and docBase indicates the document path of the Web application. -->
<Context path = "/test" docBase = "e:/webroot" debug = "0" privileged = "true">
</Context>
4) configure the Tomcat Data Source
Tomcat itself does not have the ability to provide data sources. It can be implemented using other open source data sources, such as DBCP and C3P0.
Container-managed data sources. Through the data source provided by Tomcat, the program can find the data source through JNDI to provide more convenient persistent layer access.
The data source configuration includes: global data source and local data source.
Jar files (commons-dbcp-1.2.1.jar, commons-pool-1.2.jar, commons-collections-3.1.jar) and database drivers that require DBCP data sources.
Local data source configuration: Modify the D: \ tomcat-5.0.28 \ conf \ Catalina \ localhost \ test. xml file. After 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. The Resource Name Is jdbc/dstest and the type is Data Source. -->
<Resource name = "jdbc/dstest" auth = "Container" type = "javax. SQL. DataSource"/>
<! -- Define the parameter of the resource. The name attribute specifies the parameter of the resource to be defined. -->
<ResouceParams name = "jdbc/dstest">
<! -- The following defines the parameters of the data source -->
<Parameter>
<! -- Define the data source factory -->
<Name> factory </name>
<Value> org. apach. commons. dbcp. basicperformancefactory </value>
</Parameter>
<Parameter>
<! -- Define the maximum number of active connections of the data source -->
<Name> maxActive </name>
<Value> 100 </value>
</Parameter>
<Parameter>
<! -- Defines the timeout duration of the data source. After this time, the data source is automatically disconnected. -->
<Name> removeAbandonedTimeout </name>
<Value> 60 </value>
</Parameter>
<Parameter>
<! -- Defines the maximum number of idle connections in the data source. Once the number of idle connections in the container exceeds this value, the system will automatically destroy some connections -->
<Name> maxIdle </name>
<Value> 30 </value>
</Parameter>
<Parameter>
<! -- Define the maximum number of waits for a data source -->
<Name> maxWait </name>
<Value> 10000 </value>
</Parameter>
<Parameter>
<! -- Database connection username -->
<Name> username </name>
<Value> strutsdb </value>
</Parameter>
<Parameter>
<! -- Database connection password -->
<Name> password </name>
<Value> strutsdb </value>
</Parameter>
<Parameter>
<! -- Driver used to connect to the database -->
<Name> driverClassName </name>
<Value> oracle. jdbc. driver. OracleDriver </value>
</Parameter>
<Parameter>
<! -- Connection url -->
<Name> url </name>
<Value> jdbc: oracle: thin: @ 210.45.216.146: 1521: oracle </value>
</Parameter>
<ResourceParams>
<Context>
Start Tomcat and use JNDI to access the data source. The reference code is as follows:
// Initialize Context and use InitialContext to initialize Context
Context ctx = new InitialContext ();
/**
* Query the data source through JNDI. 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 ");
// Obtain the database connection
Connection conn = ds. getConnection ();
// Get Statement
Statement stmt = conn. createStatement ();
// Execute the query and return the ResultSet object
ResultSet rs = stmt.exe cuteQuery ("select * from user ");
While (rs. next ()){
...
}
Configure all data sources: Modify the sever. xml file. For details, refer to the configuration of local data sources.
3. eclipse Environment
Install the plug-in directly: copy the plug-ins and features folders in the plug-in to the eclipse plug-ins and features folders and restart eclipse.
Extension Plugin:
1) create a new links folder in the Eclipse installation path
2) create a xxx. link file in the links folder. The file name is random, but the suffix must be link. It is recommended that the file name be the same as the plug-in name.
3) EDIT xxx. link. This file usually only needs one line of content:
Path = sync
Path = is fixed, and sync is the folder name.
4) Create an eclipse folder under the path indicated by path in the xxx. link file, and then create the plugins and features folders in the eclipse folder.
5) copy the plugins and features folders in the plug-in to the plugins and features folders created above, and then restart Eclipse.
DBCP is the most commonly used Java open-source connection pool. It is generally used in systems with frequent database usage and can process a large number of requests for database connections, it is the preferred database configuration for large sites.
Creation process of DBCP Data Source
1. Create a Data source: create a data source object ds through the BasicDataSource class of DBCP, and set the driver class, user name, password, and connection URL of the data source.
2. Close the Data source: Disable the created data source object ds.
3. test Data source: use the data source in the main () function. first, obtain the data source ds, call the getConnection function of the data source to obtain the database connection object conn, and then perform the same operations as common JDBC operations.
The test class is 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 performanceexample {
// Create a data source
Public static DataSource setupDataSource (String connectURI ){
BasicDataSource ds = new BasicDataSource ();
Ds. setDriverClassName ("oracle. jdbc. driver. OracleDriver ");
Ds. setUsername ("strutsdb ");
Ds. setPassword ("strutsdb ");
Ds. setUrl (connectURI );
Return ds;
}
// Close the data source
Public static void shutdownDataSource (DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
Bds. close ();
}
Public static void main (String [] args ){
// Create a BasicDataSource
DataSource dataSource = setupDataSource ("jdbc: oracle: thin: @ 210.45.216.146: 1521: oracle ");
// Create a JDBC object
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Try {
Conn = dataSource. getConnection ();
St = conn. createStatement ();
String SQL = "select username from loginuser ";
Rs = st.exe cuteQuery (SQL );
System. out. println ("Results :");
Int numcols = rs. getMetaData (). getColumnCount ();
While (rs. next ()){
For (int I = 1; I <= numcols; I ++ ){
System. out. println (rs. getString (I ));
}
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
Rs. close ();
St. close ();
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}
3. Use DBCP connection pool in Tomcat
Tomcat uses the DBCP database connection pool by default, the jar file required by Tomcat6.0 is: tomcat-dbcp.jar. In Tomcat6.0 using DBCP connection pool, you must follow the steps below to configure:
1. defined in Context. xml or DefaultContext. xml in Tomcat
<Context>
<! -- Default set of monitored resources -->
<WatchedResource> WEB-INF/web. xml </WatchedResource>
<! -- Uncomment this to disable session persistence resume SS 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"/>
-->
<! -- Use the data source configured by DBCP -->
<Resource name = "jdbc/struts_dbcp_connect"
Auth = "Container"
Type = "javax. SQL. DataSource"
DriverClassName = "oracle. jdbc. driver. OracleDriver"
Url = "jdbc: oracle: thin: @ 210.45.216.146: 1521: oracle"
Username = "strutsdb"
Password = "strutsdb"
MaxActive = "100"
MaxIdle = "30"
MaxWait = "10000" type = "codeph" text = "/codeph"/>
</Context>
2) In web. xml, configure the <resource-ref> element to reference the JNDI resource in the web application.
<Resource-ref>
<Description> struts dbcp connect </description>
<Res-ref-name> jdbc/struts_dbcp_connect </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
3) use data sources in Web Applications
Get reference to the Data source:
Context ctx = new InitalContext ();
DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/struts_dbcp_connect ");
Obtain the database connection object:
Connection conn = ds. getConnection ();
Back to database connection pool:
Conn. close ();
4. Configure the 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 the jar package of DBCP and the driver package of oracle.
Tomcat6.0 DBCP jar file is: tomcat-dbcp.jar.
The jar file of the oracle driver package is: C: \ oracle \ product \ 10.2.0 \ db_1 \ jdbc \ lib \ classes12.jar
2. Configure the DBCP data source in the struts-config.xml as follows:
<Struts-config>
<Data-sources>
<! -- Configure the DBCP data source in struts -->
<Data-source type = "org. apache. tomcat. dbcp. dbcp. BasicDataSource">
<Set-property = "driverClassName" value = "oracle. jdbc. driver. OracleDriver"/>
<Set-property = "url" value = "jdbc: oracle: thin: @ 210.45.216.146: 1521: oracle"/>
<Set-property = "username" value = "strutsdb"/>
<Set-property = "password" value = "strutsdb"/>
<Set-property = "maxActive" value = "10"/>
<Set-property = "maxWait" value = "5000"/>
<Set-property = "defaultAutoCommit" value = "false"/>
<Set-property = "defaultReadOnly" value = "false"/>
<Set-property = "validationQuery" value = "select count (*) FROM loginuser"/>
</Data-source>
</Data-sources>
...
</Struts-config>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.