Applet 技術已被主流瀏覽器廢棄(Chrome 45+、Firefox 52+、Edge 全版本、IE 11 僅含兼容模式),**新項目嚴禁使用**。本文僅針對必須維護的老系統,記錄曾經的跨瀏覽器部署最佳實踐:<object> (IE) + <embed> (Firefox/Chrome 老版本) 雙標簽降級。
<%@ page contentType="text/html;charset=UTF-8" %>
<%
String appletClass = "com.example.PrintApplet";
String codebase = "applets/";
String archive = "commons-logging-1.2.jar,commons-collections-3.2.2.jar,applet-core.jar";
int width = 400, height = 300;
String reportUrl = request.getRequestURL().toString();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
<title>Applet 打印組件</title>
</head>
<body>
<!-- 1. 環境檢測腳本(可選,用於動態寫入) -->
<script>
var isIE = !!window.ActiveXObject || "ActiveXObject" in window;
var isFF = navigator.userAgent.indexOf("Firefox") > -1;
</script>
<!-- 2. IE 專用 OBJECT 標簽 -->
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase="https://java.sun.com/update/1.8.0/jinstall-8u-windows-i586.cab#Version=1,8,0,0"
width="<%=width%>" height="<%=height%>">
<param name="code" value="<%=appletClass%>" />
<param name="codebase" value="<%=codebase%>" />
<param name="archive" value="<%=archive%>" />
<param name="type" value="application/x-java-applet;version=1.8" />
<param name="scriptable" value="false" />
<param name="REPORT_URL" value="<%=reportUrl%>" />
<!-- 3. 非 IE 降級 EMBED(被 IE 忽略) -->
<comment>
<embed type="application/x-java-applet;version=1.8"
pluginspage="https://java.com/download/"
code="<%=appletClass%>"
codebase="<%=codebase%>"
archive="<%=archive%>"
width="<%=width%>" height="<%=height%>"
REPORT_URL="<%=reportUrl%>"
scriptable="false">
<noembed>您的瀏覽器不支持 Java Applet,請安裝 JRE 1.8+。</noembed>
</embed>
</comment>
</object>
<!-- 4. 純非 IE 瀏覽器顯示的 EMBED(IE 已由 OBJECT 處理) -->
<!--[if !IE]>>
<embed type="application/x-java-applet;version=1.8"
pluginspage="https://java.com/download/"
code="<%=appletClass%>"
codebase="<%=codebase%>"
archive="<%=archive%>"
width="<%=width%>" height="<%=height%>"
REPORT_URL="<%=reportUrl%>"
scriptable="false">
<noembed>需安裝 JRE</noembed>
</embed>
<![endif]-->
</body>
</html>
| 屬性/標簽 | 作用 | 注意 |
|---|---|---|
classid | IE 識別 Java Plug-in 的 CLSID,固定值 | 不可變 |
codebase (OBJECT) | 指向 JRE 安裝 CAB 包,版本號需與目標 JRE 一致 | HTTPS 站點需用 HTTPS 下載鏈接 |
archive | Applet 依賴的 JAR 列表,逗號分隔,相對 codebase 路徑 | 簽名 JAR 需全部列出 |
<comment><embed></comment> | IE 會忽略 comment 內容,非 IE 解析 embed | 舊技巧,現代瀏覽器已不解析 comment 內 embed |
<!--[if !IE]><embed><![endif]--> | 條件註釋,僅非 IE 渲染 | IE10+ 不支持條件註釋,需配合 JS 動態寫入 |
scriptable | 是否允許 JavaScript 調用 Applet 公開方法 | false 更安全 |
window.print() / PDF.js;掃描 → Web TWAIN / HTML5 File API;加密簽名 → Web Crypto API / WebAuthn。Applet 時代已終結。理解歷史部署方案是為了平滑遷移,而非延續技術債。請盡快規劃替代路徑,擁抱無插件的現代 Web 標準。