Java Applets - Part I: Indian Institute of Technology Kharagpur
Java Applets - Part I: Indian Institute of Technology Kharagpur
1
Introduction
2
Design Goals of Java
¾Java is simple
It is basically a cleaned up version of C++.
Omits many rarely used and confusing features
of C++.
The size of the basic Java runtime is under 200
Kbytes.
¾Java is object-oriented
Features comparable with that of C++.
¾Java is distributed
Comes with extensive class libraries for
handling standard TCP/IP based applications.
Easy to write client-server applications.
3
¾Java is robust
Java compiler detects many errors that would
possibly show up only at run time in some
other languages.
Use of pointers is safe; we can never access a
bad pointer or make memory allocation errors.
¾Java is secure
The Java runtime disallows a number of things
(to be discussed later) that may pose security
threats.
4
¾Java is interpreted
The Java runtime (interpreter) can run on any
machine / environment where it has been
ported.
The Java runtime is not platform independent;
we need to have one for every possible
environment.
Existence of the Java runtime makes Java
byte code platform independent.
Platform Independence
5
Java Source Program
Java
Compiler
6
• A Java applet can be embedded
within a HTML document using the
<APPLET> tag.
<APPLET CODE=example.class
WIDTH=300 HEIGHT=250>
<PARAM …………>
</APPLET>
7
• Web based applications:
¾A client computer can just have the JVM.
¾Users can download and use whatever
programs/utilities they need.
A word processor.
An email client.
Game programs.
Only limited by imagination ……
• The language C# is considered as an
alternative to Java for developing Internet
applications.
Security Issues
8
• Not possible to have malicious code in
Java applets.
¾ Considered to be a plus point of Java.
¾ Other alternatives are not considered to be as
safe.
A malicious ASP code, for instance, can
access sensitive information from the local
file system.
¾Compiled as:
javac exampla.java
¾Executed as:
java example.class
9
¾A simple Java application:
10
¾A simple Java applet:
<HTML>
<APPLET CODE=example1.class,
WIDTH=150,
HEIGHT=150>
</APPLET>
………
………
</HTML>
11
The <APPLET> Tag
• General syntax:
</APPLET>
12
• The attributes:
¾ CODE: Specifies the name of the applet class file
(no subdirectories allowed).
¾ CODEBASE: Specifies a subdirectory where the
applet is to be found.
In general, can be a URL pointing to any web
server.
¾ ALT: Specifies a text message which is displayed
if the browser understands the <APPLET> tag but
cannot run the applet due to some reason.
¾ WIDTH, HEIGHT: Specify the size of the applet
display area in pixels.
13
¾ VSPACE, HSPACE: Specify in pixels the margin to
be left in the vertical and horizontal directions for
the applet.
¾ PARAM, VALUE: Used to pass parameters to an
applet from the HTML page.
14
Some Applet Examples
import java.awt.*;
import java.awt.Graphics;
import java.applet.*;
15
Example 2: play a sound clip
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
16
public boolean imageUpdate (
Image img, int infoflags,
int x, int y,
int width, int height )
{
paint (getGraphics() );
return true;
}
17
SOLUTIONS TO QUIZ
QUESTIONS ON
LECTURE 27
Example:
document.cookie = “mod_date=” +
escape (document.lastModified);
18
Quiz Solutions on Lecture 27
19
QUIZ QUESTIONS ON
LECTURE 28
20