我正在编写一个Java applet,并希望了解将其包含在网页中的最佳方式。

我希望它提示用户安装JRE,如果她还没有。 此功能应该(理想情况下)在任何运行的OS Java上进行跨浏览。 另一个要求是applet不应该在页面加载时加载,但是在用户操作之后,不要在每个页面加载时加载JVM。 我想这是SUN的官方方式,但它使用 document.write() ,所以在页面完成渲染后我无法使用它。

其他提示

我同意 jwls ,最好使用 applet 标记,因为使用embed和object非常难以获得正确的跨浏览器 - 到了自定义点每个浏览器的设置都是必要的。

但是,使用 applet 标记时,您需要注意Microsoft VM 1.1上的用户。当我在二月份测试时,他们仍然占了 5%的Java版本。如果这些用户访问需要更高版本的页面,他们将看到一个可怕的灰色区域。

解决方案(在讨论java.net之后)是使用一个小applet来检查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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top