网络客户端小程序示例
原文: https://docs.oracle.com/javase/tutorial/deployment/applet/clientExample.html
QuoteClientApplet
类允许您从与此 applet 在同一主机上运行的服务器端应用程序中获取引用。此类还显示从服务器收到的报价。
QuoteServer.java
和 QuoteServerThread.java
类构成返回报价的服务器端应用程序。这是一个包含许多引文的文本文件( one-liners.txt
)。
执行以下步骤以测试QuoteClientApplet
。
- 下载并将以下文件保存到本地计算机。
在网页中包含以下 HTML 代码以部署
QuoteClientApplet
。<script src=
"https://www.java.com/js/deployJava.js"></script>
<script>
var attributes =
{ code:'QuoteClientApplet.class', width:500, height:100} ;
var parameters =
{ codebase_lookup:'true', permissions:'sandbox' };
deployJava.runApplet(attributes, parameters, '1.6');
</script>
或者,您可以使用已包含此 HTML 代码的
quoteApplet.html
页面。- 编译
QuoteClientApplet.java
类。将生成的类文件复制到保存网页的同一目录中。 - 编译服务器端应用程序的类,
QuoteServer.java
和QuoteServerThread.java
。 - 将文件
one-liners.txt
复制到具有服务器端应用程序的类文件的目录(在上一步中生成)。 Start the server-side application.
java QuoteServer
您应该看到带有端口号的消息,如以下示例所示。记下端口号。
QuoteServer listening on port:3862
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 服务器设置而有所不同。http://JohnDoeMachine:8080/quoteApplet/quoteApplet.html
The
QuoteClientApplet
will be displayed on the web page.- 在 applet 的文本字段中输入服务器端应用程序的端口号,然后单击“确定”。显示报价。
这是 applet 的屏幕截图。
QuoteServer
Sample Output