2009-05-04 63 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> 
相關問題