网络客户端小程序示例

原文: https://docs.oracle.com/javase/tutorial/deployment/applet/clientExample.html

QuoteClientApplet 类允许您从与此 applet 在同一主机上运行的服务器端应用程序中获取引用。此类还显示从服务器收到的报价。

QuoteServer.javaQuoteServerThread.java 类构成返回报价的服务器端应用程序。这是一个包含许多引文的文本文件( one-liners.txt )。

执行以下步骤以测试QuoteClientApplet

  1. 下载并将以下文件保存到本地计算机。
  2. 在网页中包含以下 HTML 代码以部署QuoteClientApplet

    1. <script src=
    2. "https://www.java.com/js/deployJava.js"></script>
    3. <script>
    4. var attributes =
    5. { code:'QuoteClientApplet.class', width:500, height:100} ;
    6. var parameters =
    7. { codebase_lookup:'true', permissions:'sandbox' };
    8. deployJava.runApplet(attributes, parameters, '1.6');
    9. </script>

    或者,您可以使用已包含此 HTML 代码的 quoteApplet.html 页面。

  3. 编译QuoteClientApplet.java类。将生成的类文件复制到保存网页的同一目录中。
  4. 编译服务器端应用程序的类,QuoteServer.javaQuoteServerThread.java
  5. 将文件one-liners.txt复制到具有服务器端应用程序的类文件的目录(在上一步中生成)。
  6. Start the server-side application.

    1. java QuoteServer

    您应该看到带有端口号的消息,如以下示例所示。记下端口号。

    1. QuoteServer listening on port:3862
  7. Open the web page containing your applet in a browser by entering the URL of the web page. The host name in the URL should be the same as the name of the host on which the server-side application is running.

    例如,如果服务器端应用程序在名为JohnDoeMachine的计算机上运行,​​则应输入类似的 URL。确切的端口号和路径将根据您的 Web 服务器设置而有所不同。

    1. http://JohnDoeMachine:8080/quoteApplet/quoteApplet.html

    The QuoteClientApplet will be displayed on the web page.

  8. 在 applet 的文本字段中输入服务器端应用程序的端口号,然后单击“确定”。显示报价。

这是 applet 的屏幕截图。

QuoteServer Sample Output

QuoteServer Sample Output