Sie sind auf Seite 1von 6

Chapter 11

Introduction to Java Applet Programming


Chapter Objectives
Understand:
o What you can do with Java technology
o What kind of Java programs you can write
o How a Java applet is different from a Java application
o How to create and run a Java application
o The lifecycle of a Java applet and how to create and run one
o The security restrictions on a Java applet

Overview of the Chapter


While the previous chapters covered scripting languages and server-side programming, this chapter
introduces Java, a technology that includes both a programming language and a platform environment. It
is a much more comprehensive technology than the JavaScript and VBScript scripting languages and
Active Server Pages discussed in the previous chapters. Java offers more capabilities in terms of what
kind of tasks it can do compared to these other technologies.

There is one aspect of Java, namely applets, that has a relationship with HTML web pages. So this
chapter describes the structure of Java applets and how they differ from Java applications. Since Java
applets are embedded inside of an HTML page, their lifecycle and how they're invoked is not the same as
with a Java application. Java applets also have stricter security restrictions imposed upon them than what
a Java application would encounter. This is by design, in consideration of the nature of web pages and
how widely accessible they are.

Teaching Tips and Strategies


There are numerous web sites devoted to helping Java programmers with sample code and tutorials. A
Google or Yahoo search on the phrases Java tutorial or asp sample code returns thousands of sites. You
can use these code samples as lecture material to dissect and analyze or as the starting point for your
own set of modifications.

To run the Java code in this chapter, you'll need to install the Java Developer Kit (JDK). You can
download it from http://java.sun.com, under the J2SE Downloads link.

Lecture Notes
1. Getting to Know Java Technology
1.1. Two components: programming language + a hardware/operating system independent
platform
1.2. Not related to JavaScript
2. Java as a Platform
2.1. Developers write Java programs that utilize the Java API (application programming
interface)
2.2. Java API interacts with the Java VM (virtual machine)
2.3. Java VM is an application that runs on a particular hardware or operating system
platform so there are different Java VM implementations

1
2.4. Tiered architecture helps to make Java platform independent
3.

2
4. Java as a Programming Language
4.1. Uses compilation and interpretation
4.2. Java source code compiled into Java bytecodes
4.3. Java VM interprets the Java bytecodes
5. What Can Java Technology Do?
5.1. Java applets: mini-programs that run within a Java-enabled browser and have restricted
access to the host operating system facilities
5.2. Java applications: runs directly on the Java VM, does not have the access restrictions of
Java applets
5.3. Java servlets: server-based Java applications
6. Embedding an Applet into HTML
6.1. Java-enabled browser includes an integrated bytecode interpreter
6.2. Code goes inside of <applet> tag
7. The Applet Class
7.1. Embedded Java applet is an extension of the java.applet.Applet class
7.2. This class provides an interface for the code to various services and for the browser to
control the applet’s execution
8. Writing Source Code
8.1. Java applications must contain code for a method named main, which is the application’s
starting point. Java applets do not need to code this method because it’s already coded in the
Java-enabled browser
8.2. The source code file must be named classname.java where classname = the name of the
class in the source code
8.3. Most Java applets contain a paint method to define the appearance of the output text
and graphics
9. Creating a Java application
9.1. The process is basically (1) create source code, (2) compile and (3) run the class file
10. Creating a Java applet
10.1. The process is basically (1) create source code, (2) compile to create a byte-code file, (3)
create an HTML document and specify the byte-code file and (4) load the HTML document
11. Running Java Applets
11.1. Users of Java-enabled browsers can disable the running of Java applets
11.2. Several <applet> attributes are required: code, width and height
11.3. Use param attribute to pass parameters to the Java code
11.4. Use codebase attribute to specify location of the compiled Java code
12. Life Cycle of a Java Applet
12.1. java.applet.Applet class includes several methods that most applets need to override one
or more of: init, start, stop, destroy and paint.
13. Applet Security
13.1. Java applets have restricted access to the host operating system facilities in areas such
as file I/O, network connections, runtime libraries and operating system parameters
13.2. Security is specific to the Java-enabled browser so there are varying levels
13.3. Abuses are still possible even with these restrictions

Solutions to Review Exercises


Quick Check Questions
What are byte codes?
Byte codes are the output generated by the Java compiler. Unlike the case with most compilers, this
output isn't assembly language for a specific platform or host operating system. It is platform-
independent and it's a language that the Java Virtual Machine interprets.

3
Is Java a compiled or interpreted language?
The first process that takes the Java source code written by a programmer involves compilation. The
Java Virtual Machine interprets the compiler output. So Java is both a compiled and interpreted
language.

Can Java programs run on more than one operating system?


Java programs can run on more than one operating system or hardware platform as long as there's
an implementation of the Java Virtual Machine for the particular operating system or hardware
platform.

What is an API?
An API is an application programming interface. It is a mechanism for code written by a programmer
to interact with other, pre-written programming services. The Java API is a mechanism for Java
programs to utilize the services offered by the Java Virtual Machine.

What kinds of applications can you write?


Java applications can be standalone or designed to run through a web browser. Standalone
applications can offer the same functionality as non-Java programs written specifically for the
individual hardware or operating system platform. Java applications written to run through a web
browser are called applets and their capabilities are restricted to minimize risks of malicious damage.
For example, applets can't utilize the platform's file I/O or printing capabilities.

What is a Java Virtual Machine?


A Java Virtual Machine is an idealized, pseudo-operating system and hardware platform for the Java
programming environment. It is the target platform that the Java language is written for. In other
words, Java assumes it'll be running on a hardware and operating system platform represented by
the Java Virtual Machine. For input, the Java Virtual Machine reads the Java bytecodes produced by
the Java compiler, interprets these codes and calls appropriate procedures from the host hardware
and operating system that the particular Java Virtual Machine is designed to run on.

Review Questions
Review Questions
1. A program written in the Java programming language can run on any platform because...
A. Java programming is derived from C++.
B. The Java Virtual Machine1(JVM) interprets the program for the native operating system.
C. The compiler is identical to a C++ compiler.
D. The APIs do all the work.
Answer: B

2. An applet will run in almost any browser because...


A. The server has a built-in JVM.
B. The browser has a built-in JVM.
C. The source code is interpreted by the browser.
D. Applets don't need a JVM.
Answer: B

3. What is the purpose of the main method?


A. To build a user interface.
B. To hold the APIs of the application.
C. To create buttons and scrollbars.
D. To act as the entry point for the program.
Answer: D

4
4. The Applet class provides...
A. A browser to run the applet.
B. Methods to define the applet's behavior and appearance.
C. A special HTML page.
D. Permission to communicate with the server.
Answer: B

5. Which method will a web browser call first on a new applet?


A. main method
B. start method.
C. init method.
D. paint method.
Answer: C

6. Servlets are typically used for...


A. Creating graphics.
B. Extending a web server by providing dynamic web content.
C. Storing information in applets.
D. Loading buttons and menus.
Answer: B

7. Which of the following can an applet do?


A. Write a file on the applet user's hard drive
B. Access any machine on the Internet
C. Find out the username and home directory of user running an applet
D. Show a popup window without any warning messages
E. None of the above
F. All of the above
Answer: E

8. What methods must be provided in a class that subclasses Applet?


A. init, start, stop, destroy, and paint
B. init, start, stop, and destroy
C. init, start, and paint
D. start and paint
E. no methods
Answer: E

5
Lab Exercises
1. Type the following Java program into a text editor, compile and run.
public class Hello {
public static void main(String[] args){
System.out.println(“This is my first Java application”);
}
}
Remember to save the file as a text file. You must also make sure that the file name is the same as the
class name, so in this instance you must save the file as Hello.java. When you compile the program
(javac) have a look for the Java bytecode in a file called Hello.class.

2. Modify the above application by adding an if statement that tests for command-line arguments:
public class Hello {
public static void main(String[] args){
if (args.length > 0) System.out.println(args[0]);
else
System.out.println(“There were no arguments”);
}
}

3. Type the following Java program into a text editor and compile. Once the program compiles
successfully write a very short HTML file to display the applet. Use a web browser to view the HTML file.
import java.awt.*;
import java.applet.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.setFont(new Font(“Sans Serif”, Font.BOLD, 25));
g.setColor(Color.red);
g.drawString(“I was created by an applet”, 30, 30);
}
}

4. Use the Java API documentation to find out about the Font class and experiment with the available
fonts by modifying the above applet code.
Modify the above applet so that the font type is changed, the font color is blue, and two further lines of
text are neatly placed below the first.
import java.awt.*;
import java.applet.*;
public class FirstApplet extends Applet {
public void paint(Graphics g) {
g.setFont(new Font(“Sans Serif”, Font.BOLD, 25));
g.setColor(Color.blue);
g.drawString(“I was created by an applet”, 30, 30);
g.drawString(“I was created by an applet”, 30, 60);

}
}

Das könnte Ihnen auch gefallen