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

JSP 跨瀏覽器兼容 Applet 部署:OBJECT + EMBED 雙標簽降級方案

     編輯:關於JSP

Applet 技術已被主流瀏覽器廢棄(Chrome 45+、Firefox 52+、Edge 全版本、IE 11 僅含兼容模式),**新項目嚴禁使用**。本文僅針對必須維護的老系統,記錄曾經的跨瀏覽器部署最佳實踐:<object> (IE) + <embed> (Firefox/Chrome 老版本) 雙標簽降級。

完整 JSP 部署模板

<%@ 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>

關鍵點解析

屬性/標簽作用注意
classidIE 識別 Java Plug-in 的 CLSID,固定值不可變
codebase (OBJECT)指向 JRE 安裝 CAB 包,版本號需與目標 JRE 一致HTTPS 站點需用 HTTPS 下載鏈接
archiveApplet 依賴的 JAR 列表,逗號分隔,相對 codebase 路徑簽名 JAR 需全部列出
<comment><embed></comment>IE 會忽略 comment 內容,非 IE 解析 embed舊技巧,現代瀏覽器已不解析 comment 內 embed
<!--[if !IE]><embed><![endif]-->條件註釋,僅非 IE 渲染IE10+ 不支持條件註釋,需配合 JS 動態寫入
scriptable是否允許 JavaScript 調用 Applet 公開方法false 更安全

現代替代方案(必讀)

  1. WebAssembly + WASM 編譯 Java (TeaVM/CheerpJ):將 Applet 邏輯編譯為 WASM,瀏覽器原生運行,無插件。
  2. Java Web Start (JNLP) / IcedTea-Web:脫離瀏覽器啟動桌面應用,維持 Java 生態。
  3. 重寫為純 Web 技術:打印 → window.print() / PDF.js;掃描 → Web TWAIN / HTML5 File API;加密簽名 → Web Crypto API / WebAuthn。
  4. 保留舊版 IE/ESR Firefox 運行環境:虛擬機/終端服務器發布舊瀏覽器,僅供特定業務訪問。

遷移檢查清單

  • [ ] 梳理所有 Applet 功能點(打印、掃描、簽名、硬件調用)。
  • [ ] 評估各功能 Web 原生替代方案成熟度。
  • [ ] 制定分階段遷移計劃,優先替換高頻、安全風險大的功能。
  • [ ] 建立雙軌並行期,新老系統共存,驗證無誤後下線 Applet 頁面。

Applet 時代已終結。理解歷史部署方案是為了平滑遷移,而非延續技術債。請盡快規劃替代路徑,擁抱無插件的現代 Web 標準。

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