web| Program
JBuilder is an open Java IDE that integrates Tomcat, WebLogic, and other servers. Although JDK, Tomcat, and WebLogic are constantly upgraded, we can still use their latest version in JBuilder. Because the configuration of Tomcat servers is more complex, programmers accustomed to Windows platforms are often puzzled by the use of Tomcat. This paper presents an example of database Connection pool (DBCP) in a Tomcat environment, illustrates the general steps of using JBuilder to develop Web applications, and answers some of the frequently encountered problems.
JBuilder2005 's version of the JDK is 1.4.2_04-b05, whose files are placed under the directory jbuilder_home\jdk1.4, and the latest version of Tomcat is 5.0.27, with files in the directory jbuilder_home\ Thirdparty\ jakarta-tomcat-5.0.27 under the sun. The following is an example of a database Connection pool (DBCP) using a tomcat environment.
1. File-new Project new project file, input project file name MyWeb and directory C:\myWeb
2. Project-project properties set the attributes of the project file, select Tomcat as the server
3. File-new new Web Module (WAR)
Enter the name dbtest and directory dbtest for Web module
4. File-new new JSP, input JSP file name test.jsp, produce test.jsp file modify test.jsp content
TEST.JSP:
<%@ page contenttype= "text/html; Charset=big5 "%>
DB Test
<%
Foo. Dbtest TST = new Foo. Dbtest ();
Tst.init ();
%>
Results
Foo <%= Tst.getfoo ()%>
Bar <%= Tst.getbar ()%>
A runtime configuration called test will be generated.
Select Run-configurations-edit to modify runtime configuration, in particular, you can specify the port number of the server and whether to automatically search for the ports that are occupied.
5. File-new class, type name dbtest and package name foo, produce Dbtest.java file and modify its contents
Dbtest.java
Package foo;
Import javax.naming.*;
Import javax.sql.*;
Import java.sql.*;
public class Dbtest {
String foo = "Not Connected";
int bar =-1;
public void init () {
try{
Context ctx = new InitialContext ();
if (CTX = null)
throw new Exception ("boom-no context");
DataSource ds = (DataSource) ctx.lookup ("Java:comp/env/jdbc/testdb");
if (ds!= null) {
Connection conn = Ds.getconnection ();
IF (conn!= null) {
Foo = "Got Connection" +conn.tostring ();
Statement stmt = Conn.createstatement ();
ResultSet rst =stmt.executequery ("SELECT ID, foo, bar from TestData");
if (Rst.next ()) {
Foo=rst.getstring (2);
Bar=rst.getint (3);
}
Conn.close ();
}
}
}catch (Exception e) {
E.printstacktrace ();
}
}
Public String Getfoo () {return foo;}
public int Getbar () {return bar;}
}
6. Modify the Web. Content of XML
Xml:
http://java.sun.com/xml/ns/J2EE" xmlns:xsi= " http://www.w3.org/2001/ Xmlschema-instance"xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee http://java.sun.com/ Xml/ns/j2ee/web-app_2_4.xsd"version=" 2.4 ">
MySQL Test App
DB Connection
Jdbc/testdb
Javax.sql.DataSource
Container
7. F9 run the application, the Tomcat subdirectory will be generated in the MyWeb directory, which contains the Conf subdirectory,
Dbtest.xml files are generated in the Tomcat_home\conf\catalina\localhost directory
8. Add the file in the myweb\tomcat\conf directory Server8080.xml to the engineering document and modify the Server8080.xml content
Server8080.xml:
<resourceparams name= "Jdbc/testdb"
<parameter>
<name> Factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
!--
Maximum number of DB connections in pool. Make sure
Configure your MySQLd max_connections large enough to handle
All of your DB connections. Set to 0 for no limit.
-->
<parameter>
<name> maxactive </name>
<value> </value>
</parameter>
!--
Maximum number of idle DB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name> Maxidle </name>
<value> </value>
</parameter>
!--
Maximum a DB connection to become available
In MS, example seconds. An Exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
-->
<parameter>
<name> maxwait </name>
<value> 10000 </value>
</parameter>
!--MySQL DB username and password for DB connections-->
<parameter>
<name> username </name>
<value> SA </value>
</parameter>
<parameter>
<name> Password </name>
<value> Topcomputer </value>
</parameter>
!--Class name for mm.mysql JDBC driver-->
<parameter>
<name> Driverclassname </name>
<value> Com.microsoft.jdbc.sqlserver.SQLServerDriver </value>
</parameter>
!--
The JDBC connection URL for connecting to your MySQL DB.
The autoreconnect=true argument to the URL makes sure
Mm.mysql JDBC Driver'll automatically reconnect if mysqld closed the
Connection. Mysqld By default closes idle connections after 8 hours.
-->
<parameter>
<name> URL </name>
<value> jdbc:microsoft:sqlserver://nt04:1433;databasename=test </value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>
9. Place the JDBC driver in the C:\Borland\JBuilder2005\thirdparty\jakarta-tomcat-5.0.27\common\lib directory
10. Set up Database test in SQL Server, database table file TestData
Creattable.sql:
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ TestData] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [TestData]
Go
CREATE TABLE [dbo]. [TestData] (
[ID] [int] not NULL,
[Foo] [varchar] (m) COLLATE chinese_taiwan_stroke_ci_as NULL,
[Bar] [INT] Not NULL
) on [PRIMARY]
Go
Enter a few records as test data.
F9.
12. The contents of the Startup.bat and Shutdown.bat for the establishment of batch documents in C:\myWebmulu are as follows:
Startup.bat:
C:\Borland\JBuilder2005\JDK1.4\bin\Javaw-classpath "C:\Borland\JBuilder2005\thirdparty\jakarta-tomcat-5.0.27\ Bin\bootstrap.jar; C:\Borland\JBuilder2005\jdk1.4\lib\tools.jar ""-dcatalina.home=c:/borland/jbuilder2005/thirdparty/ jakarta-tomcat-5.0.27 "Org.apache.catalina.startup.bootstrap-config" C:\myWeb\Tomcat\conf\server8080. XML "Start
Shutdown.bat:
C:\Borland\JBuilder2005\jdk1.4\bin\javaw-classpath "C:\Borland\JBuilder2005\thirdparty\jakarta-tomcat-5.0.27\ Bin\bootstrap.jar; C:\Borland\JBuilder2005\jdk1.4\lib\tools.jar ""-dcatalina.home=c:/borland/jbuilder2005/thirdparty/ jakarta-tomcat-5.0.27 "Org.apache.catalina.startup.bootstrap-config" C:\myWeb\Tomcat\conf\server8080.xml "Stop"
13. Run Startup.bat, enter Http://localhost:8080/DBTest/test.JSP in Browser
How do I deploy a Web application?
1. Pack and build the war file
2. Copy Dbtest.war to Tomcat_home\webapps
3. Create a file in the Tomcat_home\conf\catalina\localhost directory Dbtest.xml
Dbtest.xml
!--
Context configuration file for the Tomcat administration Web App
$Id: Admin.xml,v 1.2 2002/07/23 12:13:05 Remm EXP $
-->
<resource name= "Jdbc/testdb" auth= "Container" type= "Javax.sql.DataSource"/>
<resourceparams name= "Jdbc/testdb"
<parameter>
<name> Factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
!--
Maximum number of DB connections in pool. Make sure
Configure your MySQLd max_connections large enough to handle
All of your DB connections. Set to 0 for no limit.
-->
<parameter>
<name> maxactive </name>
<value> </value>
</parameter>
!--
Maximum number of idle DB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name> Maxidle </name>
<value> </value>
</parameter>
!--
Maximum a DB connection to become available
In MS, example seconds. An Exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
-->
<parameter>
<name> maxwait </name>
<value> 10000 </value>
</parameter>
!--MySQL DB username and password for DB connections-->
<parameter>
<name> username </name>
<value> SA </value>
</parameter>
<parameter>
<name> Password </name>
<value> Topcomputer </value>
</parameter>
!--Class name for mm.mysql JDBC driver-->
<parameter>
<name> Driverclassname </name>
<value> Com.microsoft.jdbc.sqlserver.SQLServerDriver </value>
</parameter>
!--
The JDBC connection URL for connecting to your MySQL DB.
The autoreconnect=true argument to the URL makes sure
Mm.mysql JDBC Driver'll automatically reconnect if mysqld closed the
Connection. Mysqld By default closes idle connections after 8 hours.
-->
<parameter>
<name> URL </name>
<value> jdbc:microsoft:sqlserver://nt04:1433;databasename=test </value>
</parameter>
</ResourceParams>
</Context>
4. Start Tomcat,dbtest.war will extract to Tomcat_home\webapps\dbtest and generate DBTest directory in Tomcat_home\work\catalina\localhost directory
Why can't I generate a war file?
Set the build-related property build Web archive in the Proterties for Web Module dialog box.
How do I add directories and files to a Web application?
Right-click the module directory, select New-directory from the pop-up menu, enter the directory name, or right-click the directory where you want to create the file, select New-file in the pop-up menu, select a file type, and enter a filename. Note that the file you are adding can only be a specified file type. The directories and files that are added are packaged into the war file.
How do I add other types of files?
You can copy the file to the specified directory, set the property content in the Proterties for Web Module dialog box, select Include all classes and resources, and then package the joined files into the war file.
How do I use the specified JDK?
Select Menu Tools-configure-jdks, press the New button in the pop-up dialog box, and then select the JDK path.
Select Menu Project-project Properties and select the JDK to join in the pop-up dialog box.
How do I use the specified tomcat?
Select the menu enterprise-configure Servers, select Tomcat5.0 in the pop-up dialog box, and press the Copy button.
Select the server copy of copy of Tomcat 5.0, select Home Directory
Select Menu Project-project Properties, set property Server in the pop-up dialog box, select the joined Tomcat server