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> </server> |
9. 将jdbc驱动放在c:/borland/jbuilder2005/thirdparty/jakarta-tomcat-5.0.27/common/lib目录中
10. 在sql server中建立数据库test,数据库表文件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] (50) collate chinese_taiwan_stroke_ci_as null , [bar] [int] not null ) on [primary] go |
输入几条记录作为测试数据。
11. f9
12. 在c:/mywebmulu中建立批处理文件startup.bat和shutdown.bat内容分别如下:
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. 运行startup.bat,在浏览器输入http://localhost:8080/dbtest/test.jsp
如何部署web应用?
1. 打包生成war文件
2. 将dbtest.war拷贝到tomcat_home/webapps
3. 在tomcat_home/conf/catalina/localhost目录中建立文件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 $ --> <context path="/dbtest" docbase="/dbtest" debug="5" reloadable="true" crosscontext="true" workdir="../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> |
4. 启动tomcat,dbtest.war将会解压到tomcat_home/webapps/dbtest,并且在tomcat_home/work/catalina/localhost目录中生成dbtest目录
为什么不能生成war文件?
在proterties for web module对话框中设置与build有关的属性build web archive。
如何在web应用中加入目录和文件?
右击module directory,在弹出的菜单中选择new-directory,输入目录名称;或右击拟在其中建立文件的目录,在弹出的菜单中选择new-file,选择文件类型,输入文件名。注意这样加入的文件只能是指定的文件类型。这样加入的目录和文件都会打包到war文件中。
如何加入其它类型的文件?
可以将文件拷贝到指定的目录,在proterties for web module对话框中设置属性content,选择include all classes and resources,这样也可以将加入的文件打包到war文件中。
如何使用指定的jdk?
选择菜单tools-configure-jdks,在弹出的对话框中按new按钮,然后选择jdk的路径。
选择菜单project-project properties,在弹出的对话框中选择加入的jdk。
如何使用指定的tomcat?
选择菜单enterprise-configure servers,在弹出的对话框中选择tomcat5.0后按copy按钮,
选择copy产生的服务器copy of tomcat 5.0,选择home directory
选择菜单project-project properties,在弹出的对话框中设置属性server,选择加入的tomcat服务器
闽公网安备 35060202000074号