首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JBuilder 7 實戰:從零構建 J2EE 應用 (EJB + Servlet + 部署)

     編輯:Java 相關知識

JBuilder 7 憑藉強大的 EJB 視覺化開發工具,讓複雜的企業級應用開發變得高效直觀。本文以 JBoss + Tomcat 為運行環境,手把手示範從專案建立、EJB 模組開發、Web 應用建立、JNDI 配置到 EAR 封裝部署的完整流程。

一、環境準備與專案設定

  1. 正確安裝 JBoss + Tomcat
  2. 安裝 JBuilder 7 企業版
  3. 建立新專案,於 Project Properties > Server 選擇 JBoss 3.x
  4. 部署設定:Tools > Enterprise Deployment 配置 EAR/WAR/JAR 部署路徑

二、開發 Session EJB (以 Interest 範例為例)

  1. 取得 Interest 範例原始碼 (來自 JBoss 文檔),置於 C:\examples,移除原有 XML 配置檔
  2. File > New > Project for existing code 匯入原始碼,Rebuild
  3. New > EJB Module,命名 interest,版本選 EJB 2.0
  4. 於 EJB Designer 中點擊 Import EJB,填入:
    • Bean Name: Interest
    • Bean Type: Session
    • Bean Class: org.jboss.docs.interest.InterestBean
    • Home Interface: org.jboss.docs.interest.InterestHome
    • Remote Interface: org.jboss.docs.interest.Interest
  5. 完成後出現紅色方框表示 EJB 建立成功,可檢視 ejb-jar.xml 內容,Rebild 自動生成 JAR

三、建立 Web 應用模組

  1. New > Web Application,命名 interestweb (對應 URL: https://localhost:8080/interestweb/),目錄指向 examples\web
  2. 建立 Servlet:New > Servlet,套件 org.jboss.docs.interest,類別名 InterestServlet,複製原始邏輯碼覆蓋
  3. 建立首頁:New > JSP 命名 home.jsp,複製原 home.html 內容
  4. Rebuild 檢查無誤,自動生成 WAR

四、關鍵配置:Web 模組呼叫 EJB (JNDI 綁定)

編輯 web.xml,重點在 <ejb-ref> 區段:

<ejb-ref>
  <ejb-ref-name>ejb/Interest</ejb-ref-name>  <!-- 對應 Servlet 中 lookup 名稱 -->
  <ejb-ref-type>Session</ejb-ref-type>
  <home>org.jboss.docs.interest.InterestHome</home>
  <remote>org.jboss.docs.interest.Interest</remote>
  <ejb-link>Interest</ejb-link>  <!-- 對應 ejb-jar.xml 中 ejb-name -->
</ejb-ref>

注意:<url-pattern> 需與前端表單 action 大小寫一致 (範例中修正為 /InterestServlet);設定 welcome-file-list 為 home.jsp。

五、封裝 EAR 並部署至 JBoss

  1. New > EAR,嚮導自動偵測 EJB 模組與 Web 模組,勾選加入
  2. 完成後生成 application.xml (對應專案樹 examples.eargrp)
  3. Rebuild 生成 examples.ear
  4. 部署:Tools > Enterprise Deployment 發布至 JBoss,或手動複製至 JBoss/server/default/deploy

六、驗證測試

瀏覽 https://localhost:8080/interestweb/,輸入參數提交,若顯示:

Servlet interface to EJB
Calling EJB...
Interest on 1000.0 units, at 10.0% per period, compounded over 2.0 periods is: 210.00000000000023

即代表 J2EE 應用成功建置部署。此流程展示 JBuilder 7 將繁瑣的 XML 配置、封裝部署自動化,讓開發者專注業務邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved