murach’s beg. java 2, jdk 5, c18© 2005, mike murach & associates, inc.slide 1

34
Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 1 C hapter18 How to develop applets

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 1

Chapter 18

How to develop applets

Page 2: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 2

Objectives

Applied

Given a working Swing application, convert the application to an applet.

Write a simple HTML page that displays an applet.

Use the Applet Viewer to test an applet.

Use the Java Plug-in HTML Converter to convert an HTML page that includes an APPLET tag to a form that can be run by Internet Explorer or Netscape browsers.

Run an applet in a browser window by opening an HTML page that includes the applet.

Use the jar command to create an archive file that contains an applet, and modify the HTML file that displays the applet so the applet is run from the jar file.

Page 3: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 3

Objectives (continued) Given the specifications for a program that uses the Swing

components presented in this book, implement the program as an applet.

Knowledge

Explain the difference between a Java applet and an application.

Describe what happens if a user tries to run an applet without having the appropriate Java Plug-in installed.

Describe the operations applets are not allowed to do for security reasons.

List the four methods defined by the Applet class and explain when each of them is called.

Explain how to modify a Swing application so that it runs as an applet.

Page 4: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 4

Objectives (continued) List three attributes that are commonly used with the APPLET

tag.

Explain the purpose of the Applet Viewer.

Explain the purpose of the Java Plug-in HTML Converter.

Explain the purpose of the Java Console window and describe how to display it with version 5.0 or later of the JDK.

Explain the benefits of using jar files to deploy applets.

Page 5: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 5

The Future Value Calculator applet

Page 6: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 6

How applets work An applet is a special type of application that can be downloaded

from an Internet or intranet server and run on the client’s computer within a web browser.

The Java Plug-in is included as part of the JRE. It is a browser plug-in that allows a web browser to use a version of the JRE that’s newer than the one that’s included with the browser.

For a client machine to run applets, a current version of the JRE and Java Plug-in must be installed on the client.

If a user attempts to run an applet without first installing the current version of the Java Plug-in, a prompt will be displayed indicating that the Java Plug-in must be downloaded.

Page 7: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 7

Applet security issues To prevent applets from damaging a client system or from making

it possible to damage a client system, security restrictions limit what an applet can do.

To overcome these security restrictions, you can create a signed applet. This indicates that the applet comes from a trusted source. Then, you can add rights to the signed applet.

Page 8: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 8

What an applet can’t do Read, write, or delete files or databases on the client system.

Access information about the files or databases on the client system.

Run programs on the client system.

Access system properties for the client system except the Java version, the name and version of the operating system, and the characters used to separate directories, paths, and lines.

Make network connections to other servers available to the client system.

Page 9: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 9

What an applet can do Display user interface components and graphics.

Send keystrokes and mouse clicks back to the applet’s server.

Make network connections to the applet’s server.

Call public methods from other applets on the same web page.

Page 10: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 10

The inheritance hierarchy for a Swing applet java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet

Four methods of the Applet class

Method Description

public void init() Called when the browser first loads the applet.

public void start() Called after the init method and every time the user moves to the web page for the applet.

public void stop() Called before the destroy method and every time the user moves to another web page.

public void destroy() Called when the user exits the browser.

Page 11: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 11

The inheritance hierarchy and methods of an applet To create a Swing applet, you define a class that extends the

JApplet class.

Then, you can override the init, start, stop, and destroy methods of the Applet class as needed.

These methods are called automatically by the browser, and they control the execution of the applet.

Page 12: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 12

The code for the Future Value Calculator applet import java.awt.*; import javax.swing.*; public class FutureValueApplet extends JApplet { public void init() { JPanel panel = new FutureValuePanel(); this.add(panel); } }

Page 13: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 13

How to modify the code for the FutureValuePanel class so it can be used with an applet public class FutureValuePanel extends JPanel implements ActionListener { // the declarations should not include an Exit button public FutureValuePanel() { // the constructor should not create an Exit button } public void actionPerformed(ActionEvent e) { // the ActionListener should not respond to the // Exit button } }

Page 14: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 14

How to code the HTML page for an applet The Hypertext Markup Language (HTML) is the language that’s

used to create web pages.

Each HTML tag begins with the tag name and ends with the tag name prefixed by a forward slash.

Within a tag, you can set the attributes for the tag.

Although HTML isn’t case-sensitive, the name of the Java applet class is.

Page 15: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 15

An HTML page that includes the Future Value Calculator applet <html> <head> <title>Future Value Calculator applet</title> </head> <body> <h1>Future Value Calculator applet</h1> <applet code = "FutureValueApplet.class" width = 240 height = 175> <!-- If the browser can't display the applet, display this HTML --> <p>This applet requires version 1.5 or later of Java.</p> </applet> </body> </html>

Page 16: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 16

Some HTML tags

Tag Description

<html></html> Marks the beginning and end of an HTML document.

<head></head> Marks the beginning and end of the head of the HTML document.

<title></title> Defines the title that’s displayed in the title bar of the browser.

<body></body> Marks the beginning and end of the body of the HTML document.

<h1></h1> Displays the enclosed text as a level-1 header.

<p></p> Displays the enclosed text as a paragraph.

Page 17: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 17

Some HTML tags (continued)

Tag Description

<applet></applet> Defines an applet within the HTML page. After you code this tag, you can use the HTML Converter to convert this tag to OBJECT and EMBED tags.

<object></object> Defines an object within the HTML page. Internet Explorer uses this tag to define applets.

<embed></embed> Embeds an application within the HTML page. Netscape uses this tag to define applets.

Page 18: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 18

Some attributes for working with the APPLET tag

Attribute Description

code Specifies the class name of the code to be executed.

width Specifies the width of the applet in pixels.

height Specifies the height of the applet in pixels.

An HTML comment <!-- This is an HTML comment -->

Page 19: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 19

An applet in the Applet Viewer

How to test an applet with the Applet Viewer The Applet Viewer that’s included in the JDK lets you test an

applet before you run it in a browser.

When you run the Applet Viewer, you will see the applet but you won’t see any other elements that are defined by the HTML page.

Page 20: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 20

How to run the Applet Viewer from TextPad Open the source code file for the applet. Then, select the Run Java

Applet command from the Tools menu or press Ctrl+3.

If you have not created an HTML page for the applet or the page is stored in a different directory than the applet, TextPad will automatically create a temporary HTML page for you. Otherwise, the Applet Viewer will use the existing HTML page.

How to run the Applet Viewer from the command prompt Start the command prompt, and navigate to the directory that

contains the HTML page you want to test. Then, enter the appletviewer command followed by the name of the HTML page: C:\java1.5\ch18\FutureValue>appletviewer future_value.html

Page 21: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 21

The HTML Converter dialog box

Page 22: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 22

How to use the HTML Converter with Windows 1. Open the Windows Explorer and navigate to your JDK’s bin folder

(typically C:\Program Files\Java\jdk1.5.0\bin).

2. Double-click the HtmlConverter.exe file to display the Java Plug-in HTML Converter dialog box.

3. Use the HTML Converter dialog box to specify the HTML files that you want to convert, along with the backup directory for the original HTML files if necessary.

4. Click the Convert button to convert the files.

Page 23: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 23

Options in the HTML Converter Dialog Box You can use the Matching File Names text field to limit or expand

the types of files that the HTML Converter attempts to convert.

You can use the Include Subfolders check box to include all subfolders in the conversion.

You can use the Template File combo box to create HTML files that work with different browsers and operating systems.

You can use the Java Versioning radio button to create HTML files that help the user automatically or manually download the appropriate version of the JRE.

Page 24: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 24

An HTML file after the conversion process <html> <head> <title>Future Value Calculator applet</title> </head> <body> <h1>Future Value Calculator applet</h1> <!--"CONVERTED_APPLET"--> <!-- HTML CONVERTER --> <object classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=1,5,0,0" WIDTH = 240 HEIGHT = 175 > <PARAM NAME = CODE VALUE = "FutureValueApplet.class" > <param name = "type" value = "application/x-java-applet;version=1.5"> <param name = "scriptable" value = "false">

Page 25: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 25

An HTML file after the conversion process (continued) <comment> <embed type = "application/x-java-applet;version=1.5" \ CODE = "FutureValueApplet.class" \ WIDTH = 240 \ HEIGHT = 175 scriptable = false pluginspage = "http://java.sun.com/products/plugin/index.html#download"> <noembed> <p>This applet requires version 1.5 or later of Java.</p> </noembed> </embed> </comment> </object> <!-- <APPLET CODE = "FutureValueApplet.class" WIDTH = 240 HEIGHT = 175> <p>This applet requires version 1.5 or later of Java.</p> </APPLET> --> <!--"END_CONVERTED_APPLET"--> </body> </html>

Page 26: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 26

How the HTML Converter affects an HTML file It inserts HTML comments to show where the conversion begins

and ends.

It converts the APPLET tag to the OBJECT and EMBED tags that are used by the Internet Explorer and Netscape browsers, respectively.

It stores the original APPLET tag within an HTML comment.

It uses capital letters for attributes that were coded in the original APPLET tag and lowercase letters for other tags that were generated.

Page 27: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 27

A Swing applet with the Java Console displayed

Page 28: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 28

How to test a Swing applet 1. Start your web browser, display the applet’s HTML page by

entering its address, and test the applet to make sure it’s working correctly.

2. If necessary, display the Java Console to view the output from println statements or information about any exceptions that have been thrown.

How to display the Java Console If you’re using version 5.0 or later of the JDK, right-click the Java

icon in the task bar and select the Show Console command.

Page 29: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 29

The syntax for using the JAR tool at the command line

jar [options] JARFileName File1 File2 File3 ...

Common options of the JAR tool

Option Description

c Creates a new JAR file and adds the specified files to it.

f Specifies the second argument as the JAR file name.

u Updates an existing JAR file by adding or replacing files.

x Extracts the files from the specified JAR file.

t Lists the contents of the specified JAR file.

v Creates verbose output that includes compression information about the files that are stored in a JAR file.

Page 30: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 30

How to create and work with JAR files To use the JAR tool, start the command prompt and navigate to

the directory where the files you want to archive are stored. Then, issue the JAR command.

To operate on all the files with a given extension, you can use the wildcard character (*).

Page 31: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 31

An example that creates a JAR file C:\java1.5\ch18\FutureValue>jar cfv FutureValue.jar *.class added manifest adding: FinancialCalculations.class(in = 437) (out= 336)(deflated 23%) adding: FutureValueApplet.class(in = 452) (out= 298)(deflated 34%) adding: FutureValuePanel.class(in = 2966) (out= 1583)(deflated 46%)

An example that updates a JAR file C:\java1.5\ch18\FutureValue>jar ufv FutureValue.jar SwingValidator.class adding: SwingValidator.class(in = 2022) (out= 1010)(deflated 50%)

Page 32: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 32

An example that extracts files from a JAR file C:\java1.5\ch18\FutureValue>jar xfv FutureValue.jar created: META-INF/ inflated: META-INF/MANIFEST.MF inflated: FinancialCalculations.class inflated: FutureValueApplet.class inflated: FutureValuePanel.class inflated: SwingValidator.class

An example that lists the contents of a JAR file C:\java1.5\ch18\FutureValue>jar tf FutureValue.jar META-INF/ META-INF/MANIFEST.MF FinancialCalculations.class FutureValueApplet.class FutureValuePanel.class SwingValidator.class

Page 33: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 33

The ARCHIVE attribute of the APPLET tag

Attribute Description

archive Specifies the archive file (such as a JAR file) to be downloaded.

How to include a JAR file that contains an applet in an HTML page To improve the download time for your applets, you can place all

the files needed by the applet in one JAR file.

Then, you can use the ARCHIVE attribute of the APPLET tag to specify the name of the JAR file.

Page 34: Murach’s Beg. Java 2, JDK 5, C18© 2005, Mike Murach & Associates, Inc.Slide 1

Murach’s Beg. Java 2, JDK 5, C18 © 2005, Mike Murach & Associates, Inc. Slide 34

An HTML page that uses the ARCHIVE attribute to specify a JAR file <html> <head> <title>Future Value Calculator applet</title> </head> <body> <h1>Future Value Calculator applet</h1> <applet archive = "FutureValueApplet.jar" code = "FutureValueApplet.class" width = 240 height = 175> <!-- If the browser can't display this applet, display this --> <p>This applet requires version 1.5 or later of Java.</p> </applet> </body> </html>