Java was once thought to be the dominant web format, but nowadays it works more in the background than in the front-end. However, you still see some Java applets embedded in web pages from time to time, although the Java plug-in lacks the large penetration of the Flash plug-in. When a Java applet is embedded in a page, you can call all public methods of this applet. The following, very simple applet exposes its setString() method, which sets a private variable and then repaints the applet: A Simple Java Applet (JavaApplet.java)
After this applet is compiled into a class, the following JavaScript code calls the setString() method whenever the content of the text field changes. Figure 12.3 shows the result. Accessing Java with JavaScript (java.html)
Figure 12.3. The applet changes while you are typing. |