Develop web applications with JBuilder2005
Gaoke Hua
JBuilder is an open Java IDE that integrates Tomcat, WebLogic. Although JDK, Tomcat, WebLogic continues to upgrade, we can still use their latest versions in JBuilder. Since the TOMCAT server is complicated, the programmers who are used to the Windows platform are often confused for Tomcat. This article gives an example of a database connection pool Database Connection Pool (DBCP) in the Tomcat environment, which illustrates the general steps for developing web applications with JBuilder and answers some of the frequent problems.
The JBUILDER2005 version of JDK is 1.4.2_04-b05, and its file is placed under the directory jbuilder_home / jdk1.4, the latest version of Tomcat is 5.0.27, its file is placed on the directory jbuilder_home / thirdParty / Jakarta-Tomcat-5.0.27 under. The following will first give an example of a database connection pool Database Connection Pool (DBCP) using the Tomcat environment.
1. File-New Project New Project Document, Enter Project File Name MYWEB and Directory C: / MyWeb
2. Project-Project Properties Set the properties of the project file, select Tomcat to the server
3. File-New New Web Module (WAR)
Enter the name of the Web Module DBTEST and Directory DBTest
4. File-new JSP, enter the name of the JSP file Test.jsp, and modify the contents of Test.jsp after the test.jsp file
Test.jsp:
<% @ Page ContentType = "Text / HTML; Charset = BIG5"%>
hEAD>
<%
Foo.dbtest TST = New foo.dbtest ();
TST.INIT ();
%>
FOO <% = TST.GETFOO ()%>
Bar <% = TST.GetBar ()%>
body>
html>
A Runtime Configuration named Test will be generated.
The Run-Configurations-Edit can modify the Runtime Configuration, especially if you specify the port number of the server and whether it is automatically searched as the port occupied.
5. File-new class, enter the class DBTEST and package name foo, modify its content after the dbtest.java file is generated
Dbtest.java
Package foo;
Import javax.naming. *;
Import javax.sql. *;
Import java.sql. *;
Public class dbtest {
String foo = "not connection";
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.toT7tring ();
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 content of web.xml
Web.xml:
XML Version = "1.0" encoding = "UTF-8"?>
resource-ref>
web-app>
7. F9 running the app, the Tomcat subdirectory will be generated in the MyWeb directory, which contains the conf subduial directory.
Generate dbtest.xml files in Tomcat_Home / Conf / Catalina / LocalHost directory
8. Add file server8080.xml in myWeb / Tomcat / conf directory to the project file, modify the content of Server8080.xml
Server8080.xml:
XML Version = "1.0" encoding = "UTF-8"?>
parameter>
Maximum Number of DB Connections in pool. Makess
Configure your mysqld max_connections large enough to handle
All of Your DB Connections. Set to 0 for no limit.
->
parameter>
Maximum Number of iDLE DB Connections To retain in pool.
Set to 0 for no limit.
->
parameter>
Maximum Time to Wait for A DB Connection To Become Availablein MS, IN this Example 10 seconds. AN Exception is throwniff
THIS TIMEOUT IS EXCEEDED. SET to -1 to wait indefinitely.
->
parameter>
parameter>
parameter>
parameter>
The JDBC Connection Url for Connecting to your My My Mysql DB.
The autoreconnect = true argument to the url makess sure what the the URL MAKES SURE THAT
MM.MYSQL JDBC Driver Will Automatically Reconnect IF MySQLD CLOSED THE
Connection. mysqld by Default Closes iDle Connections After 8 Hours.
->
parameter>
Resourceparams>
Context>
Host>
Engine>
Service>
Server>
9. Put the JDBC drive in a C: /borland/jakarter2005/thirdparty/jakarta-tomcat-5.0.27/common/lib directory
10. Create a database TEST in SQL Server, Database Table File TestData
CreatTable.sql:
IF exists (select * from dbo.sysObjects where id = Object_id (n '[dbo]) And ObjectProperty (id, n'susertable') = 1)
Drop Table [DBO]. [TestData]
Go
Create Table [DBO]. [TestData] (
[ID] [int] Not null,
[foo] [varchar] (50) collate chinese_taiwan_stroke_ci_as null, [bar] [int] not null
) On [primary]
Go
Enter several records as test data.
11. f9
12. Create a batch file in C: / MyWebmulu Startup.bat and shutdown.bat contents 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 / confected /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 / confected /server8080.xml "stop
13. Run Startup.bat, enter http:// localhost: 8080 / dbtest / test.jsp: // localhost: 8080 / dbtest / test.jsp
How to deploy web applications?
1. Package the generated WAR file
2. Copy dbtest.war to tomcat_home / webApps
3. Create file dbtest.xml in tomcat_home / conf / catalina / localhost directory
Dbtest.xml
Context Configuration File for the Tomcat Administration Web APPP
$ ID: admin.xml, v 1.2 2002/07/23 12:13:05 REMM EXP $
->
parameter>
Maximum Number of DB Connections in pool. Makess
Configure your mysqld max_connections large enough to handle
All of Your DB Connections. Set to 0 for no limit.
->
parameter>
Maximum Number of iDLE DB Connections To retain in pool.
Set to 0 for no limit.
->
parameter>
Maximum Time to Wait for A DB Connection To Become Available
IN ms, in this example 10 seconds. An Exception Is Thrown IF
THIS TIMEOUT IS EXCEEDED. SET to -1 to wait indefinitely.
->
parameter>
parameter>
parameter>
parameter>
The JDBC Connection Url for Connecting to your My My Mysql DB.
The autoreconnect = true argument to the url makess supething
Connection. mysqld by Default Closes iDle Connections After 8 Hours.
->
parameter>
Resourceparams>
Context>
4. Start Tomcat, dbtest.war will decompress Tomcat_Home / WebApps / Dbtest, and generate dbtest directories in Tomcat_Home / Work / Catalina / LocalHost directory
Why can't I generate a WAR file?
Set the properties related to Build Web Archive in the Proterties for Web Module dialog.
How do I join directory and files in a web application?
Right-click Module Directory, select New-Directory in the pop-up menu, enter the directory name; or right-click on the directory to create the file, select New-file in the pop-up menu, select the file type, and enter the file name. Note that this added file can only be the specified file type. The directory and files thus added will be packaged into the WAR file.
How to add other types of files?
You can copy the file to the specified directory, set the properties Content in the Proterties for Web Module dialog, select Include All Classes and Resources, which can also package the added file into the WAR file.
How to use the specified JDK?
Select Menu Tools-Configure-JDKS, press the New button in the pop-up dialog box and select the path to the JDK.
Select Menu Project-Project Properties, select Add JDK in the pop-up dialog box.
How to use the specified Tomcat?
Select Menu Enterprise-Configure Servers, select Tomcat5.0 in the pop-up dialog box, press the COPY button,
Select Copy's Server Copy Of Tomcat 5.0, select Home Directory
Select Menu Project-Project Properties, set attribute Server in the pop-up dialog, select the join Tomcat server