我同意 jwls,最好使用 applet 标签,因为使用 embed 和 object 很难获得正确的跨浏览器 - 以至于自定义每个浏览器都需要设置。
但是,使用 applet 标签时,您需要注意 Microsoft 的 VM 1.1 上的用户。当我在二月份进行测试时,它们仍然占5% of Java versions。如果这些用户访问需要更新版本的页面,他们将看到一个可怕的灰色区域。
解决方案(在 java.net 上讨论后)是使用一个小程序来检查 Java 版本并在不满足目标版本时重定向到失败页面。这是我的来源:
JavaRedirectorApplet.java
import java.applet.Applet;
import java.net.URL;
/**
* Applet built for bytecode 1.1
*
* If applet is less than a set level redirects to a given page, else does nothing
*/
public class JavaRedirectorApplet extends Applet {
/** The required java version */
private final static String PARAM_REQUIRED_JAVA_VERSION = "REQUIRED_JAVA_VERSION";
/** The failure page */
private final static String PARAM_FAILURE_PAGE = "FAILURE_PAGE";
/**
* Initializes the applet
*/
public void init() {
// evaluate the required Java version
double requiredJavaVersion = -1;
String requiredJavaVersionString = getParameter(PARAM_REQUIRED_JAVA_VERSION);
if (requiredJavaVersionString != null) {
try {
requiredJavaVersion = Double.valueOf(requiredJavaVersionString).doubleValue();
} catch (Exception e) {
// ignored, caught below
}
}
if (requiredJavaVersion < 0) {
System.err.println(PARAM_REQUIRED_JAVA_VERSION + " not set or set incorrectly (must be set to a number greater than 0)");
return;
}
// get the failure page
URL failurePageURL = null;
String failurePageString = getParameter(PARAM_FAILURE_PAGE);
if (failurePageString != null) {
try {
failurePageURL = new URL(getCodeBase().getProtocol(),
getCodeBase().getHost(),
getCodeBase().getPort(),
failurePageString);
} catch (Exception e) {
// ignored, caught below
}
}
if (failurePageURL == null) {
System.err.println(PARAM_FAILURE_PAGE + " not set or set incorrectly (must be set to a valid path)");
return;
}
// check to see whether valid
if (!isValidVersion(requiredJavaVersion)) {
// not valid redirect self
getAppletContext().showDocument(failurePageURL, "_self");
}
// seems fine
}
/**
* Check the Java version against a required version
*
* @param versionRequired
* @return the verdict
*/
public static boolean isValidVersion(double versionRequired) {
try {
double javaVersion = Double.valueOf(System.getProperty("java.version").substring(0, 3)).doubleValue();
if (javaVersion < versionRequired) {
return false;
} else {
return true;
}
} catch (NumberFormatException e) {
return false;
}
}
}
示例 HTML
<!-- place before the actual applet -->
<div style="display: none;">
<applet code="JavaRedirectorApplet" width="0" height="0">
<param name="REQUIRED_JAVA_VERSION" value="1.4"/>
<param name="FAILURE_PAGE" value="/failurePage.html" />
</applet>
</div>