AppletJava小应用程序是一种在Web环境下,运行于客户端Java程序组件。它也是1990年代中期,Java在诞生后得以一炮走红的功臣之一。通常,每个Applet的功能都比较单一(例如仅用于显示一个舞动的Logo),因此它被称作“小应用程序”1JDK 9以後不再支援。

Applet必须运行于某个特定的“容器”,这个容器可以是浏览器本身,也可以是通过各种插件,或者包括支持Applet的移动设备在内的其他各种程序来运行。与一般的Java应用程序不同,Applet不是通过main方法来运行的(参见Java的Hello World程序Java Applet的Hello World程序)。在运行时Applet通常会与用户进行互动,显示动态的画面,并且还会遵循严格的安全检查,阻止潜在的不安全因素(例如根据安全策略,限制Applet对客户端文件系统的访问)。

2016年,甲骨文公司宣布Applet只保留至JDK 8,JDK 9以後的版本不再支援,因為2013年開始主流的瀏覽器考量安全穩定因素,漸漸不支援外掛程式如Applet。大部分瀏覽器2021年已完全不支援外掛程式。[1][2][3][4]

應用範例 编辑

  • AWT方式撰寫一個顯示「Hello, world!」的Java applet。
import java.applet.Applet;
import java.awt.*;

// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class HelloWorld extends Applet {
  // This method is mandatory, but can be empty (i.e., have no actual code).
  public void init() { }

  // This method is mandatory, but can be empty.(i.e.,have no actual code).
  public void stop() { }

  // Print a message on the screen (x=20, y=10).
  public void paint(Graphics g) {
    g.drawString("Hello, world!", 20,10);
  
  // Draws a circle on the screen (x=40, y=30).
    g.drawArc(40,30,20,20,0,360);
  }
}
  • 上述Java的Code編譯成HelloWorld.class,再透過下述網頁使用。
<!DOCTYPE HTML PUBLIC 
  "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML>
<HEAD>
<TITLE>HelloWorld_example.html</TITLE>
</HEAD>
<BODY>
<H1>A Java applet example</H1>
<P>Here it is: <APPLET code="HelloWorld.class" WIDTH="200" HEIGHT="40">
This is where HelloWorld.class runs.</APPLET></P>
</BODY>
</HTML>

注释 编辑

  1. Applet是由英语应用程序Application的缩写App和代表“小”的后缀let组成。ServletServer-let)、MIDletMobile Information Device-let)和JSP中的Scriptlet的命名也是基于同样原理。

参见 编辑

参考文献 编辑

  1. ^ Oracle Announces End Of Java Applet Support. [2022-12-20]. (原始内容存档于2022-12-20). 
  2. ^ Chrome 不再支援 NPAPI (Java Applet 所需的技術). [2022-12-20]. (原始内容存档于2022-12-20). 
  3. ^ Oracle Java SE Support Roadmap). [2022-12-20]. (原始内容存档于2020-11-29). 
  4. ^ NPAPI). [2022-12-20]. (原始内容存档于2023-01-18).