Beruflich Dokumente
Kultur Dokumente
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.
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
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.
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.
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
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
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);
}
}