Java Reference
In-Depth Information
4.6
Applets
Applets can perform network communication using TCP/IP sockets, but there are severe
restrictions on how and with whom they can converse. Without such restrictions, unsuspecting
Web browsers might execute malicious applets that could, for example, send fake email,
attempt to hack other systems while the browser user gets the blame, and so on. These security
restrictions are enforced by the Java security manager, and violations by the applet result in
a SecurityException . Typically, browsers only allow applets to communicate with the host
that served the applet. This means that applets are usually restricted to communicating with
applications executing on that host, usually a Web server originating the applet. The list of
security restrictions and general applet programming is beyond the scope of this topic. It is
worth noting, however, that the default security restrictions can be altered, if allowed by the
browser user.
Suppose that you wanted to implement an applet that allowed users to type and save notes
to themselves on their browser. Browser security restrictions prevent applets from saving data
directly on the local file system, so you would need some other means besides local disk I/O
to save the notes. FileClientApplet.java (available from the topic's Web site) is an applet that
allows the user to type text into an editor window and, by clicking the “Save” button, copy the
text over the network to a server (running on port 5000). The server, TCPFileServer.java (also
on the topic's Web site), saves the data to a file. It takes a port (use 5000 to work with the applet)
and the name of the file. The server must execute on the Web server that serves the applet to
the browser. Note that there is nothing applet specific about the server. FileClientApplet.html
on the Web site demonstrates how to integrate the applet into a Web page. Be warned that the
applet is based on Swing, and most browsers don't have the Swing library. The HTML file should
download the necessary file to make this work, but it is not guaranteed.
4.7
Wrapping Up
We have discussed some of the ways Java provides access to advanced features of the sockets
API, and how built-in features such as threads can be used with socket programs. In addition
to these facilities, Java provides several mechanisms that operate on top of TCP or UDP and
attempt to hide the complexity of protocol development. For example, Java Remote Method
Invocation (RMI) allows Java objects on different hosts to invoke one another's methods as
if the objects all reside locally. The URL class and associated classes provide a framework
for developing Web-related programs. Many other standard Java library mechanisms exist,
providing an amazing range of services. These mechanisms are beyond the scope of this topic;
however, we encourage you to look at the topic's Web site for descriptions and code examples
for some of these libraries.
4.8
Exercises
1. State precisely the conditions under which an iterative server is preferable to a multipro-
cessing server.
Search WWH ::




Custom Search