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> |
| 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;} } |
| <?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> |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器