Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java™ and Web Browsers Relating versions Applets P Browsers must be Java™-aware or support a JVM plug-in P Netscape Chapter 10 < 2.0 is Java™ 1.0 aware < 4.0 or 4.1 claimed to be Java™ 1.1 aware < 4.5 is Java™ 1.1 aware < 4.5 (maybe earlier versions) supports JVM plug-ins P Internet Explorer < 3.0 is Java™ 1.0 aware < 4.0 is Java™ 1.1 aware < Modern versions (exact version is unknown) support JVM plug-ins via ActiveX Copyright © 1998-2002 Delroy A. Brinkerhoff. All Rights Reserved. Chapter 10 Slide 1 of 24 Chapter 10 Slide 2 of 24 Competing Tags HTML Files Running Java™ on a web page To launch Java™ applets (pre HTML 4.0) <HTML> <HEAD><TITLE> Java Applet Test </TITLE></HEAD> <BODY> P <APPLET> tag formally adopted at HTML 3.2 < Supported by Netscape 2.0 < Supported by Internet Explorer 3.0 < Deprecated at HTML 4.0 P Netscape introduced <EMBED> tag P Internet Explorer introduced <OBJECT> tag P HTML adopted the <OBJECT> at HTML 4.0 P <OBJECT> was expanded to work with applets, plug-ins, ActiveX, images, etc. P <OBJECT> may not be supported by all browsers yet Chapter 10 Slide 3 of 24 required tags { <APPLET code="WinHello.class" codebase="relativePath/" width="100" height="100" > </APPLET> </BODY> </HTML> codebase is not required if .html & .class file are in the same directory Chapter 10 Slide 4 of 24 HTML Files Applets and JAR Files To launch Java™ applets (post HTML 4.0) Jar files improve applet efficiency (p. 625 and Chapter 1, Slide 16) required tags { <HTML> <HEAD><TITLE> Java Applet Test </TITLE></HEAD> <BODY> <OBJECT classid="java:WinHello.class" codetype="application/octet-stream" codebase="relativePath/" width="100" height="100" > </OBJECT> P Each .class file (or other resource file) requires a separate HTTP request (a connection is opened, the file transferred, and the connection closed) P Placing all files in a Jar reduces the overhead of multiple connections <APPLET code="WinHello.class" archive="Hello.jar" codebase="relativePath/" width="100" height="100" > </APPLET> </BODY> </HTML> codebase is not required if .html & .class file are in the same directory Chapter 10 Slide 5 of 24 Chapter 10 Slide 6 of 24 Java™ Plug-In HTMLConverter Separating the JVM and the browser Loading the Java™ plug-in from an HTML page P Free from Sun P Packaged with the JRE < http://java.sun.com/products/plugin/ < htmlconv13.zip < Add unzip directory to classpath < cd to html directory < java HTMLConverter < http://java.sun.com/products/plugin/ < May be redistributed < Required for Java™ 1.2 (and newer) features, including Swing < Located in the “Control Panel” (1.3+) < Located in “Programs” (1.2) Chapter 10 Slide 7 of 24 Chapter 10 Slide 8 of 24 Viewing Applets Applet Execution Options Appletviewer, Netscape, Internet Explorer, and HotJava™ A summary P Applet is started through an HTML file P View with applet viewer: appletviewer file.html P If a browser is registered to handle HTML files, double click P File | Open Page Chapter 10 Slide 9 of 24 Appletviewer Netscape <APPLET> / 1.1 Okay Okay <OBJECT> / 1.1 Fail Okay <APPLET> / 1.2+ Okay Fail <APPLET> / 1.1 Converted Fail Okay <APPLET> / 1.2+ Converted Fail Okay Chapter 10 Applet Basics Applet Example Browser interpreted java programs “Hello world” on the world wide web P Are based on the AWT import java.applet.Applet; import java.awt.Graphics; < import java.applet.Applet; < import java.awt.*; < import java.awt.event.*; < extends Applet class // if GUI based P Are started in an HTML file with the <APPLET> tag (deprecated at HTML 4.0) or the <OBJECT> tag < code < codebase < width < height < align applet file name relative path (from html file) to applet (.class file) width of applet window in pixels or percent height of applet window in pixels or percent left or right (several other also available) Slide 10 of 24 public class WinHello extends Applet { public void paint(Graphics g) // most work done here { g.drawString("Hello world", 10, 20); } } P Are interpreted by a browser or appletviewer Chapter 10 Slide 11 of 24 Chapter 10 Slide 12 of 24 Swing Applet Basics Swing Applet Example Browser plug-ins Similar to a Swing application import java.awt.*; import javax.swing.*; P Based on awt and on Swing < import java.awt.*; < import javax.swing.*; < import java.awt.event.*; < import javax.event.*; < extends JApplet class public class JWinHello extends JApplet { public void init( ) { Container content = getContentPane( ); content.add(new SketchPanel( )); } } // if GUI based // may be needed P Started with <applet> class SketchPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello world", 10, 20); } } < run HTMLConverter < added tags load the plug-in P Browser loads the JVM as a plug-in Chapter 10 Slide 13 of 24 Chapter 10 Slide 14 of 24 Arguments from HTML to Applet Example of APPLET and PARAM Configurable applets Starting Java™ applets from HTML files P <APPLET> and </APPLET> (or <OBJECT> and </OBJECT>) tags surround < 0 or more <PARAM> tags < Default text displayed if the browser is not Java™ aware <APPLET code=WinMoney.class height=300 width=300> <PARAM name = "html_money" value = "217"> P <PARAM> tags < Used to pass parameters or arguments to applets < Attribute: name=”variableName” < Attribute: value=”variableValue” < For example: <PARAM name=”html_money” value=”217”> Output from a Java applet showing 217 pennies as an equivalent ount in dollars, quarters, dimes, nickels, and pennies P Applet reads parameters with the getParameter method < Returns a String value, which is converted if necessary < String Name = getParameter(”variableName”); < int money = Integer.parseInt(getParameter(”html_money”).trim()); Chapter 10 <HTML> <HEAD><TITLE> Convert Pennies </TITLE></HEAD> <BODY> Slide 15 of 24 </APPLET> </BODY> </HTML> Chapter 10 Example Java™ Applet From AWT Applications to Applets Convert pennies to higher denominations Step by step conversion P Create an HTML page with an Applet or Object tag P Eliminate main (not used in applets) import java.applet.*; import java.awt.*; public class WinMoney extends Applet { int money, dollars, quarters, dimes, nickels; // instance vars public void init( ) { money = Integer.parseInt(getParameter(”html_money”).trim()); calculate(money); } Chapter 10 Slide 16 of 24 Slide 17 of 24 < Remove setSize < Remove setTitle(); (use the <title> tag instead) < Remove setLocaton if called < Remove addWindowListener(this) and related code P Derive your class from Applet not Frame < import java.applet.*; P Rename the constructor to public void init() P If the frame implicitly uses a BorderLayout (the default for a Frame), you must explicitly add a BorderLayout to the applet P Applets cannot have menus Chapter 10 Slide 18 of 24 From Swing To JApplets Dual-Mode Programs Similar to Applet conversions Programs that are applications and applets P Create an HTML page with an Applet tag // main from SpirographConsole-- still extends Applet < Process the HTML file with HTMLConverter < Make sure the plug-in is installed public static void main(String args[ ]) { Frame f = new SpiroFrame(); // extends Frame SpirographConsole console = new SpirographConsole(); P Eliminate main (not used in applets) < Remove setSize < Remove setTitle( ); (use the <title> tag instead) < Remove setLocaton if called < Remove addWindowListener(this) and related code P Derive your class from JApplet not JFrame P Rename the constructor to public void init() P BorderLayout it the default for JFrame and JApplet P JApplets can have menus Chapter 10 } Slide 19 of 24 f.add("Center", console); f.setSize(600, 400); f.setTitle("Spirograph Console"); console.init(); f.show(); // should follow init Chapter 10 Slide 20 of 24 Dual-Mode Programs Applet Security Summary Continued Restrictions increase from applications to down-loaded applets import java.awt.*; import java.awt.event.*; public class SpiroFrame extends Frame implements WindowListener { public SpiroFrame( ) { addWindowListener(this); } public void windowClosing(WindowEvent e) { System.exit(0); } public void windowClosed(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } // class SpiroFrame Chapter 10 Slide 21 of 24 Capabilities JA AV BF BU Read local file U U Y Y Write local file U U Y Y Get file information U U Y Y Delete file U Y Y Y Run program U U Y Y Read the users.name property U U U Y Connect to network port on server U U U U Connect to network port on other host U U U Y Load Java library U U U Y Call exit Create pop-up window BU: Browser loading URL AV: Applet Viewer Chapter 10 U U Y U U U BF: Browser loading local file JA: Java application (stand-alone) Displaying New Web Pages Connecting An Applet to its Host Like a hypertext link in an applet “Applets can only phone home” Slide 22 of 24 P URL(String url) P URL(URL base, String relName) P Member methods // only runs through a web browser import java.net.*; import java.applet.*; < String < String < int < String < InputStream public class Display extends Applet { public void init() { try { URL location = new URL("http://icarus.weber.edu/home/dab/"); AppletContext context = getAppletContext(); context.showDocument(location); } catch (MalformedURLException E) { System.err.println("URL error " + E); } } } // class Display Chapter 10 Y Warn getHost( ) getFile( ) getPort( ) getProtocol( ) openStream( ) name of host serving the URL gets file name of URL P Related java.Applet methods < public URL getDocumentBase( ) < public URL getCodeBase( ) P URL source = new URL(getDocumentBase(), "employe.dat"); Slide 23 of 24 Chapter 10 Slide 24 of 24