jbuilder是一个开放的java ide,它集成了tomcat、weblogic等服务器。虽然jdk、tomcat、weblogic不断升级,我们仍可以在jbuilder中使用它们的最新版本。由于tomcat服务器的配置比较复杂,习惯了windows平台的程序员常常对tomcat的使用感到困惑。本文给出了一个使用tomcat环境下的数据库连接池database connection pool (dbcp) 的例子,说明了用jbuilder开发web应用的一般步骤,并回答了一些经常遇到的问题。
jbuilder2005所带jdk的版本是1.4.2_04-b05,其文件放在目录jbuilder_home/jdk1.4下,tomcat的最新版本是5.0.27,其文件放在目录jbuilder_home/thirdparty/ jakarta-tomcat-5.0.27下。下面首先给出给出了一个使用tomcat环境下的数据库连接池database connection pool (dbcp) 的例子。
1. file-new project新建工程文件,输入工程文件名称myweb和目录c:/myweb
2. project-project properties设置工程文件的属性,选择tomcat为服务器
3. file-new新建web module(war)输入web module的名称dbtest和目录dbtest
4. file-new新建jsp,输入jsp文件的名称test.jsp,产生test.jsp文件后修改test.jsp的内容
test.jsp:
<%@ page contenttype="text/html; charset=big5" %>
<html>
<head>
<title>db test</title>
</head>
<body>
<%
foo.dbtest tst = new foo.dbtest();
tst.init();
%>
<h2>results</h2>
foo <%= tst.getfoo() %>
bar <%= tst.getbar() %>
</body>
</html>
将会生成一个名称为test的runtime configuration。选run-configurations-edit可修改runtime configuration,特别是可以指定服务器的端口号和是否自动搜索为被占用的端口。
5. file-new class,输入类名dbtest和包名foo,产生dbtest.java文件后修改它的内容
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. 修改web.xml的内容
web.xml:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="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">
<description>mysql test app</description>
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/testdb</res-ref-name>
<res-type>javax.sql.datasource</res-type>
<res-auth>container</res-auth>
</resource-ref>
</web-app>
7. f9运行应用,myweb目录中将会生成tomcat子目录,其中包含了conf子目录,在tomcat_home/conf/catalina/localhost目录中生成了dbtest.xml文件
8. 将myweb/tomcat/conf目录中的文件server8080.xml加入工程文件,修改server8080.xml的内容
server8080.xml:
<?xml version="1.0" encoding="utf-8"?>
<server debug="0" port="8081" shutdown="shutdown">
<service name="catalina">
<connector acceptcount="10" connectiontimeout="60000" debug="0" maxthreads="75" minsparethreads="5" port="8080"/>
<engine debug="0" defaulthost="localhost" name="catalina">
<host appbase="c:/myweb/tomcat/webapps" autodeploy="false" debug="0" deployxml="false" name="localhost" unpackwars="false">
<context path="/dbtest" docbase="c:/myweb/dbtest" debug="5" reloadable="true" crosscontext="true" workdir="c:/myweb/tomcat/work/dbtest">
<logger classname="org.apache.catalina.logger.filelogger" prefix="localhost_dbtest_log." suffix=".txt" timestamp="true"/>
<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 you
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>100</value>
</parameter>
<!--
maximum number of idle db connections to retain in pool.
set to 0 for no limit.
-->
<parameter>
<name>maxidle</name>
<value>30</value>
</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>
<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 that the
mm.mysql jdbc driver will 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