国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

利用JBuilder2005開發Web應用程序

2024-07-21 02:14:59
字體:
來源:轉載
供稿:網友

  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() %><br/>
 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服務器

  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 云梦县| 新民市| 铜鼓县| 太康县| 成都市| 黎平县| 永昌县| 兖州市| 永宁县| 永善县| 伊宁市| 青田县| 曲麻莱县| 兰溪市| 嘉兴市| 南安市| 交城县| 灵丘县| 太湖县| 东乡族自治县| 额敏县| 拜城县| 天峻县| 江西省| 永年县| 阿坝县| 山西省| 连平县| 惠东县| 县级市| 巨鹿县| 宝清县| 柳河县| 古交市| 兰坪| 民乐县| 普洱| 德清县| 翁源县| 巍山| 道真|