2009-05-04 44 views
2

我正在写一个Java小程序,并希望知道将其包含在网页中的最佳方式。我如何提示用户安装JRE,如果她还没有安装它?

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

+0

这不就是浏览器的功能?像“您缺少查看此页面所需的插件”? – 2009-05-04 15:29:51

回答

4

我会建议只使用小程序标记。正如Alex B所评论的,如果他们没有JRE,大多数浏览器都会提示用户在那一刻安装。

即使Sun建议使用using the applet tag except on Intranets,除非您在Intranet上。我想这里的逻辑是你可以在内部服务器上托管JRE下载,并使用嵌入的对象标签来指示下载到该服务器。

我曾经使用嵌入&对象标签,但最终由于版本号而变得麻烦。假设你需要Java 1.5。因此,您可以在对象&中指定嵌入标记,以确保用户如果不具有1.5,则必须升级。但是,这并不是您想要的,您希望它将其升级到最新的JVM。无论如何,当我最后一次使用它时,并不是最聪明的行为 - 现在他们可能已经改进了它。

0

jwls同意,这是更好,因为采用内嵌式设计,目的是非常尴尬的得到正确的跨浏览器使用的小程序标签 - 到每个浏览器自定义安装越来越必要了点。

但是使用小程序标记,您需要注意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> 
相关问题