技術棧環境
- JBuilder 7 / WebLogic 6.1 / Oracle 8i
- 目標:Stateless Session Bean → WebLogic JDBC Connection Pool → Oracle → Servlet 客戶端
階段一:JBuilder 7 與 WebLogic 6.1 整合配置
- 伺服器配置:
Tools → Configure Servers → WebLogic 6.x+
☑ Enable Server
Home Directory: D:/bea/wlserver6.1
Work Directory: D:/bea/wlserver6.1
Custom 頁籤 → 輸入 WebLogic 啟動密碼
(注意:螢幕解析度需 1024x768 才能看見 OK 鈕)
- Enterprise Setup - CORBA:
Tools → Enterprise Setup → CORBA
New → 名稱: WebLogic
Path for ORB tools: WebLogic 安裝目錄
Library for project: weblogic 6.x deploy library
Compiler command: idcj.exe
Output Directory: 自訂輸出路徑
- 專案屬性確認:
Projects → Default Project Properties → Server
Application Server: WebLogic 6.x
- 系統環境變數修正:
TEMP / TMP → 改為無空格路徑 (如 C:\temp)
(JBuilder 對路徑空格敏感)
階段二:WebLogic 建立 Oracle 連線池
- WebLogic Console → Services → JDBC → Connection Pools → Configure a new Connection Pool
- 關鍵配置:
Name: OraclePool
Database Type: Oracle
Driver: Oracle's Driver (Thin) / (Type 2)
URL: jdbc:oracle:thin:@host:1521:SID
Properties: user=scott; password=tiger
Initial Capacity: 5
Maximum Capacity: 20
Capacity Increment: 2
Test Table: SELECT 1 FROM DUAL
Test Connections On Reserve: true
- 建立 DataSource 映射:
Services → JDBC → DataSources → Configure a new DataSource
Name: OracleDS
JNDI Name: jdbc/OracleDS
Pool Name: OraclePool
階段三:無狀態 Session Bean 開發
package com.example.ejb;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.DataSource;
import java.sql.*;
public class QueryBean implements SessionBean {
private SessionContext ctx;
private DataSource ds;
public void setSessionContext(SessionContext ctx) {
this.ctx = ctx;
try {
Context init = new InitialContext();
ds = (DataSource) init.lookup("java:comp/env/jdbc/OracleDS");
} catch (NamingException e) { throw new EJBException(e); }
}
public String getProductName(String productId) {
String sql = "SELECT PRODUCT_NAME FROM PRODUCTS WHERE PRODUCT_ID = ?";
try (Connection conn = ds.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, productId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) return rs.getString(1);
}
} catch (SQLException e) { throw new EJBException(e); }
return null;
}
// EJB 生命週期方法
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
}
階段四:ejb-jar.xml 部署描述符
QueryBean
com.example.ejb.QueryHome
com.example.ejb.QueryRemote
com.example.ejb.QueryBean
Stateless
Container
jdbc/OracleDS
javax.sql.DataSource
Container
QueryBean
*
Required
階段五:WebLogic 專用 weblogic-ejb-jar.xml
QueryBean
jdbc/OracleDS
jdbc/OracleDS
階段六:Servlet 客戶端呼叫
public class ProductServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
Context init = new InitialContext();
QueryHome home = (QueryHome)
init.lookup("java:comp/env/ejb/QueryBean");
QueryRemote bean = home.create();
String name = bean.getProductName(req.getParameter("id"));
res.getWriter().println("Product: " + name);
} catch (Exception e) {
throw new ServletException(e);
}
}
}
階段七:web.xml Servlet 與 EJB 參考宣告
ProductServlet
com.example.web.ProductServlet
ProductServlet
/product
ejb/QueryBean
Session
com.example.ejb.QueryHome
com.example.ejb.QueryRemote
jdbc/OracleDS
javax.sql.DataSource
Container
關鍵驗證點
- Connection Pool 測試:Console → Pool → Test Pool
- DataSource JNDI 綁定:Console → DataSources → View JNDI Tree
- EJB 部署:Console → Deployments → EJB Modules → 測試 Home/Remote
- Servlet 存取:
https://localhost:7001/app/product?id=1001
常見問題排查