[ Pobierz całość w formacie PDF ]
.The only extra thing that you must do in your client program is to look up and fetch the remote interface from the server.From then on, its just regular Java programming: sending messages to objects.Heres the program that uses PerfectTime://: c15:rmi:DisplayPerfectTime.java// Uses remote object PerfectTime.// {Broken}package c15.rmi;import java.rmi.*;import java.rmi.registry.*;public class DisplayPerfectTime {public static void main(String[] args)throws Exception {System.setSecurityManager(new RMISecurityManager());PerfectTimeI t =(PerfectTimeI)Naming.lookup("//peppy:2005/PerfectTime");for(int i = 0; i < 10; i++)System.out.println("Perfect time = " +t.getPerfectTime());}} ///:~The ID string is the same as the one used to register the object with Naming, and the first part represents the URL and port number.Since youre using a URL, you can also specify a machine on the Internet.What comes back from Naming.lookup( ) must be cast to the remote interface, not to the class.If you use the class instead, youll get an exception.You can see in the method callt.getPerfectTime()that once you have a reference to the remote object, programming with it is indistinguishable from programming with a local object (with one difference: remote methods throwSummaryThis chapter has introduced some, but not all, of the components that Sun refers to as J2EE: the Java 2 Enterprise Edition.The goal of J2EE is to create a set of tools that allows the Java developer to build server-based applications more quickly than before, and in a platform-independent way.Its not only difficult and time-consuming to build such applications, but its especially hard to build them so that they can be easily ported to other platforms, and also to keep the business logic separated from the underlying details of the implementation.J2EE provides a framework to assist in creating server-based applications; these applications are in demand now, and that demand appears to be increasing.ExercisesSolutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.l Compile and run the JabberServer and JabberClient programs inthis chapter.Now edit the files to remove all of the buffering for the inputand output, then compile and run them again to observe the results.Commentll Create a server that asks for a password, then opens a file and sends thefile over the network connection.Create a client that connects to this server,gives the appropriate password, then captures and saves the file.Test the pairof programs on your machine using the localhost (the local loopback IPaddress 127.1 produced by callingInetAddress.getByName(null)).Commentll Modify the server in Exercise 2 so that it uses multithreading to handlemultiple clients.Commentll Modify JabberClient.java so that output flushing doesnt occurand observe the effect.Commentll Modify MultiJabberServer so that it uses thread pooling.Instead of throwing away a thread each time a client disconnects, the threadshould put itself into an available pool of threads.When a newclient wants to connect, the server will look in the available pool for a threadto handle the request, and if one isnt available, make a new one.Thisway the number of threads necessary will naturally grow to the requiredquantity.The value of thread pooling is that it doesnt require theoverhead of creating and destroying a new thread for each new client.Commentll Starting with ShowHTML.java, create an applet that is apassword-protected gateway to a particular portion of your Web site.Commentll Modify CIDCreateTables.java so that it reads the SQL strings from atext file instead of CIDSQL.Commentll Configure your system so that you can successfully executeCIDCreateTables.java and LoadDB.java.Commentll (More challenging) Take the VLookup.java program and modify it sothat when you click on the resulting name it automatically takes that name andcopies it to the clipboard (so you can simply paste it into your email).Youll need to look back at Chapter 13 to remember how to use theclipboard in JFC.Commentl This means a maximum of just over four billion numbers, which is rapidly running out.The new standard for IP addresses will use a 128-bit number, which should produce enough unique IP addresses for the foreseeable future.Created by Dave Bartlett.Many brain cells died in agony to discover this information
[ Pobierz całość w formacie PDF ]