JavaBean 是 JSP 開發中封裝業務邏輯與數據存取的核心組件。本文透過完整範例,示範如何建立符合規範的 JavaBean,並在 JSP 頁面中使用 <jsp:useBean> 與 <jsp:getProperty> 標籤來調用其屬性,實現伺服器端系統資訊的動態顯示。
首先定義封裝於 beanpackage 套件下的 SystemProperties 類別,提供多個 getter 方法讀取 JVM 與作業系統層級的系統屬性:
package beanpackage;
/**
* 系統屬性存取 Bean
*/
public class SystemProperties {
public String getRuntimeEnvironmentVersion() {
return System.getProperty("java.version");
}
public String getRuntimeEnvironmentVendor() {
return System.getProperty("java.vendor");
}
public String getVmVersion() {
return System.getProperty("java.vm.version");
}
public String getVmVendor() {
return System.getProperty("java.vm.vendor");
}
public String getVmName() {
return System.getProperty("java.vm.name");
}
public String getVmSpecVersion() {
return System.getProperty("java.vm.specification.version");
}
public String getVmSpecVendor() {
return System.getProperty("java.vm.specification.vendor");
}
public String getVmSpecName() {
return System.getProperty("java.vm.specification.name");
}
public String getRteSpecName() {
return System.getProperty("java.specification.name");
}
public String getRteSpecVersion() {
return System.getProperty("java.specification.version");
}
public String getRteSpecVendor() {
return System.getProperty("java.specification.vendor");
}
public String getOperatingSystemName() {
return System.getProperty("os.name");
}
public String getOperatingSystemVersion() {
return System.getProperty("os.version");
}
public String getOperatingSystemArchitecture() {
return System.getProperty("os.arch");
}
}
在 JSP 中引入 Bean 並輸出屬性值:
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"https://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="properties" class="beanpackage.SystemProperties"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP 系統屬性示範</title>
</head>
<body>
<p>作業系統名稱:<jsp:getProperty name="properties" property="operatingSystemName"/><br></p>
<p>作業系統版本:<jsp:getProperty name="properties" property="operatingSystemVersion"/><br></p>
<p>作業系統架構:<jsp:getProperty name="properties" property="operatingSystemArchitecture"/><br></p>
<p>執行環境版本 (JDK):<jsp:getProperty name="properties" property="runtimeEnvironmentVersion"/><br></p>
<p>執行環境廠商:<jsp:getProperty name="properties" property="runtimeEnvironmentVendor"/><br></p>
<p>VM 版本:<jsp:getProperty name="properties" property="vmVersion"/><br></p>
<p>VM 廠商:<jsp:getProperty name="properties" property="vmVendor"/><br></p>
<p>VM 名稱:<jsp:getProperty name="properties" property="vmName"/><br></p>
<p>RTE 規格版本:<jsp:getProperty name="properties" property="rteSpecVersion"/><br></p>
<p>RTE 規格廠商:<jsp:getProperty name="properties" property="rteSpecVendor"/><br></p>
<p>RTE 規格名稱:<jsp:getProperty name="properties" property="rteSpecName"/><br></p>
<p>VM 規格名稱:<jsp:getProperty name="properties" property="vmSpecName"/><br></p>
<p>VM 規格版本:<jsp:getProperty name="properties" property="vmSpecVersion"/><br></p>
<p>VM 規格廠商:<jsp:getProperty name="properties" property="vmSpecVendor"/><br></p>
</body>
</html>
部署至 Tomcat 後,瀏覽器將顯示類似以下資訊:
作業系統名稱:Windows Vista 作業系統版本:6.0 作業系統架構:x86 執行環境版本 (JDK):1.6.0_16 執行環境廠商:Sun Microsystems Inc. VM 版本:14.2-b01 VM 廠商:Sun Microsystems Inc. VM 名稱:Java HotSpot(TM) Client VM RTE 規格版本:1.6 RTE 規格廠商:Sun Microsystems Inc. RTE 規格名稱:Java Platform API Specification VM 規格名稱:Java Virtual Machine Specification VM 規格版本:1.0 VM 規格廠商:Sun Microsystems Inc.
<jsp:useBean> 的 class 屬性需指定完整類別名稱(含套件)。<jsp:getProperty> 的 name 需與 useBean 的 id 一致,property 對應 Bean 的屬性名(首字母小寫)。