JSP 與 ASP 相似,皆在 HTML 中嵌入程式碼由引擎解釋執行。JSP 使用 Java,具備跨平台、物件導向、豐富類庫優勢。本文採用 TRS (拓爾思) 全文檢索引擎搭配 TRS JavaBeans,構建高效能查詢網站。
startWebLogic.cmd,設定 PRE_CLASSPATH 包含 TRSBean.jar、TRSDemo.jar 完整路徑weblogic.properties:
weblogic.httpd.register.servlets=weblogic.servlet.ServletServletweblogic.httpd.register.*.jsp、weblogic.servlet.JSPServlet、weblogic.httpd.initArgs.*.jsp 前的 #compileCommand 指向實際 JDK javac.exe 路徑DEMO 目錄複製至 D:/weblogic/myserver/public_html資料庫:news.北京新聞,欄位:標題、內容、日期、編號 (唯一鍵)。
<HTML>
<HEAD>
<TITLE>新聞詳情</TITLE>
<link REL="stylesheet" href="mycss.css" TYPE="text/css">
<META http-equiv="Content-Type" content="text/html; charset=GB2312">
<%@ page import="com.eprobiti.TRS.*" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="TRSConn" scope="session" class="com.eprobiti.TRS.TRSConnection" />
<jsp:useBean id="TRSRS" scope="page" class="com.eprobiti.TRS.TRSResultSet" />
</HEAD>
<BODY>
<%
String item_id = request.getParameter("id"); // 從 URL 或表單隱藏域取得
String dbname = "news.北京新聞";
String ip = "202.123.166.99";
String port = "8888";
String username = "yourname";
String password = "yourpassword";
String filter = "編號=" + item_id; // 檢索條件
try {
if (TRSConn.connect(ip, port, username, password)) {
TRSRS = TRSConn.executeSelect(dbname, filter, "", "", null, 0, 0, false);
} else {
out.println("TRS 連接失敗!");
}
} catch (TRSException e) {
out.println("TRS 連接異常:" + e.getMessage());
}
try {
TRSRS.moveFirst(); // 定位第一筆
%>
<p><center>
<table width="80%">
<tr align="center"><td><%=TRSRS.getString("標題")%></td></tr>
<tr align="center"><td><%=TRSRS.getString("日期")%></td></tr>
<tr><td><%=TRSRS.getString("內容")%></td></tr>
</table>
</center></p>
<%
TRSRS.close();
} catch (TRSException e) {
out.println("資料讀取異常:" + e.getMessage());
}
%>
</BODY>
</HTML>
| 類別/方法 | 用途 |
|---|---|
TRSConnection.connect(ip, port, user, pwd) | 建立與 TRS 伺服器連線 |
TRSConnection.executeSelect(db, filter, sort, group, fields, start, count, isHighlight) | 執行檢索,回傳 TRSResultSet |
TRSResultSet.moveFirst()/moveNext() | 游標定位 |
TRSResultSet.getString(fieldName) | 取得欄位字串值 |
TRSResultSet.close() | 釋放資源 |
executeSelect 的 start、count 參數實現分頁。filter 使用 TRS 查詢語法 (關鍵字、AND/OR/NOT、欄位限定)。