📄 ch3.htm
字号:
<P>If a required applet is not located on your system, it can bedownloaded automatically to your system and then run. To the user,this exchange of applets over the Internet is mostly transparent.All the user knows is that she's looking at a page that containsa game of TicTacToe, an animated image of Duke, or some otherJava-based content. In this way, the Internet becomes almost anextension of the user's basic system, sort of a gigantic harddrive that contains a practically infinite number of accessibleapplets and applications.<P>Currently, there's a client/server relationship between a browserthat wants to display an applet and the system that can supplythe applet. The client is a computer that requires services fromanother system; the server is the computer that provides thoseservices. In the case of a Java applet, the client is the computerthat's trying to display an HTML document that contains a referenceto an applet. The server is the computer system that uploads theapplet to the client, thereby allowing the client to use the applet.In Figure 3.2, you could call the local computer the client andthe remote computer the server.<P>It won't be long, however, before the difference between a clientand a server begins to get muddy. When Java browsers can sendas well as receive applets, computers will constantly switch betweenbeing a client and a server. For example, suppose a user loadsup his favorite Java-compatible browser and connects to a Website. The home page on the Web site contains an animated title,so your system downloads the applet that displays this title.For the time being, your system is the client and the remote systemis the server.<P>Now, however, you decide that you want to search the remote system'spublic databases for a particular file. Because you've just writtena handy search application that can do the job for you, your systemtransmits the application to the remote computer, where it setsto work finding the file you specified. Suddenly, your computeris the server and the remote computer is the client.<P>This sort of switching between client and server tasks is a steptoward making the Internet a huge extension of your computer.That is, more and more, the Internet will seem to be a part ofyour own local system, rather than a collection of computers locatedall over the world. You'll be able to access the Internet almostas easily as your own hard drive. In fact, you might not evenneed a hard drive at all! You can just run applications locatedsomewhere else on the Internet and store your data in any numberof special storage sites.<H2><A NAME="Security"><FONT SIZE=5 COLOR=#Ff0000>Security</FONT></A></H2><P>You may have heard horror stories about people who have downloadedprograms from the Internet only to find, after running the program,that it infected their system with a virus or otherwise wreakedhavoc with their computer. Therefore, you may be reluctant tojump on the applet bandwagon. After all, with so many appletsflying around the Internet, trouble could rear its ugly head likea demon from a Clive Barker movie.<P>The truth, however, is that Java applets are a secure way to transmitprograms on the Internet. This is because the Java interpreterwill not allow an applet to run until the interpreter has confirmedthat the applet's byte-code has not been corrupted or changedin some way (Figure 3.3). Moreover, the interpreter determineswhether the byte-code representation of the applet sticks to allof Java's rules. For example, a Java applet can never use a pointerto gain access to portions of computer memory for which it doesn'thave access. The bottom line is that, not only are Java appletssecure, they are virtually guaranteed not to crash the system.<P><A HREF="f3-3.gif"><B> Figure 3.3 : </B><I>Applets are verified before they are run, so they are virtually guaranteed to be safe and secure.</I></A><P><H2><A NAME="ExampleYourPagesontheWeb"><FONT SIZE=5 COLOR=#Ff0000>Example: Your Pages on the Web</FONT></A></H2><P>Because you bought this book to learn how to write applets, you'reprobably also interested in setting up your own Web pages. (Ofcourse, you may be interested only in creating applets that otherpeople can use. That's okay, too.) To set up your own Java-compatibleWeb site, you need to create publicly accessible folders on yourhard drive. You also need to gather all the applets you need soyou can store them together in one place on your hard drive. Thatis, you'll want your Web pages to contain local applets. The instructionsin this section will get you started on organizing a public folder.<P>First, create a folder on your hard drive's root directory. Namethis folder something like PUBLIC. The PUBLIC folder will containall of the files that are accessible to Web users who connectto your pages. Inside the PUBLIC folder, create a folder calledsomething like APPLETS. The APPLETS folder will contain the appletsthat are referenced in your Web pages.<P>Now that you have your folders created, copy all the applets youneed into the APPLETS folder. The applets' .CLASS files shouldbe in the APPLETS directory, with their support files (such asgraphics and sounds) in appropriately named folders within theAPPLETS directory. For example, if you wanted to use the BouncingHeadsapplet in one of your pages, you'd copy the contents of the JAVA\DEMO\BOUncINGHEADSfolder to your APPLETS folder, ending up with the directory structureshown in Figure 3.4.<P><A HREF="f3-4.gif"><B> Figure 3.4 : </B><I>You need to set up your public directories properly.</I></A><P><P>The next step is to create the HTML files for your pages. Whenyou've written these pages, they should be placed in the PUBLICfolder. Listing 3.3 shows the HTML file for a simple home pagethat displays the BouncingHeads applet and enables the user toview the applet's source code. The corresponding Web page is shownin Figure 3.5. Notice that the user can view the applet's sourcecode by clicking on the link at the bottom of the page. When shedoes, she sees a window similar to Figure 3.6.<P><A HREF="f3-5.gif"><B> Figure 3.5 : </B><I>A simple, Java-powered home page.</I></A><P><P><A HREF="f3-6.gif"><B> Figure 3.6 : </B><I>The user can view the applet's source code by clicking the link.</I></A><P><HR><BLOCKQUOTE><B>Listing 3.3 HOMEPAGE.htmL: A Home Page Displayinga Java Applet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Big Benny</title><h1>Welcome to Big Benny's Home Page</h1><b>We're hot wired for Java!</b><p><applet codebase="applets" code="BounceItem.class" width=400 height=150></applet><p><a href="applets/BounceItem.java">The source code</a>for this applet is available for your viewing pleasure.</PRE></BLOCKQUOTE><HR><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Generally, you do not need to copy Java source code files to your APPLETS folder. Source code is not required in order to load and run applets.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>By placing all the publicly accessible files in one root directory,you can more easily set up your system's security to ensure thatthe rest of your system is kept safe from prying eyes. Moreover,you know exactly where you need to store any additional filesthat you may add to your site. Exactly how you create the foldersfor your Web pages will depend upon the applets and files youneed to use, but the previous example should get you going fairlyeasily.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Of course, to create a Web site, you need to be connected to the Internet either directly or through an Internet provider that enables you to set up your own Web pages. If you are currently unable to have your own Web site, you can still set one up on your hard drive and then use your Java-compatible Web browser to load and view your Web pages. Then, when you get your Internet access, you'll be all ready to go. To find more information on setting up your own Web site, crank up your browser and log onto the handy Lycos directory at <TT><A TARGET="resource window" HREF="http://www.lycos.com">http://www.lycos.com</A></TT>; then fish around under the Computers, Web Publishing & HTML subdirectory.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>Thanks to a Java-compatible browser's ability to download applets,users who log onto Java-powered Web pages can enjoy the Java experiencewithout even realizing what's going on behind the scenes. Thisis unlike other types of applications on the Internet that theuser must explicitly download before they can be run. When moreand more applets start running rampant on the Information Superhighway,the Internet will become a virtual extension of your own computersystem, one that's almost as easily accessed as your local harddrive. When this happens, the Internet will start living up toits hype.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What is a local applet?<LI>What is a remote applet?<LI>Explain the client/server relationship as it applies to Javaapplets.<LI>How will the client/server focus of the Internet change asapplications start to flow two ways, both from and to a remotecomputer?<LI>Explain why Java applets are secure and guaranteed not tocrash the system on which they're executing.</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Add additional applets to the simple Web site you set up inthis chapter. You can copy the applets from the JAVA\DEMO folder.<LI>Modify the HOMEPAGE.htmL file to display the additional appletsyou added in exercise 1.<LI>Log onto the Internet and then find and download additionalapplets that you can use in your own pages. Start off at thisURL: <TT><A TARGET="resource window" HREF="http://www.javasoft.com/applets/appletsites.html">http://www.javasoft.com/applets/appletsites.html</A></TT></OL><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>, All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -