Java Applets: Kuldeep Yogi
Java Applets: Kuldeep Yogi
Kuldeep Yogi
Introduction
Java applets are one of three kinds of
Java programs:
– An application is a standalone program that
can be invoked from the command line.
– An applet is a program that runs in the
context of a browser session.
– A servlet is a program that is invoked on
demand on a server program and that runs in
the context of a web server process.
Applets, web page, client, server
• Applets are programs stored on a web server, similar to web
pages.
• When an applet is referred to in a web page that has been
fetched and processed by a browser, the browser generates a
request to fetch (or download) the applet program, then
executes the program in the browser’s execution context, on
the client host.
server ho st
bro w ser ho st
w eb serve r
brow se r
re qe u st fo r
m y W e b P a g e .h tm l
m y W e b P a g e .h tm l
m y W e b P a g e .h tm l H e llo W o r ld .c la s s
... re qu e st fo r
< a p p le t c o d e = H e llo W o r ld .c la s s < /a p p le t> H e llo W o r ld c la s s
...
H e llo W o r ld .c la s s H e llo W o r ld .c la s s
Applet Execution - 1
• An applet program is written as a subclass of
the java.Applet class or the
javax.swing.Japplet class. There is no main
method: you must override the start method.
Applet objects uses AWT for graphics.
JApplet uses SWING.
• It is a Graphics object that runs in a Thread
object, so every applet can perform graphics,
and runs in parallel to the browser process.
Applet Execution
• When the applet is loaded, these methods are automatically
invoked in order:
– the init( ) method is invoked by the Java Virtual Machine.
– The start( ) method
– The paint( ) method.
• The applet is now running and rendered on the web page.
• You program the start( ) method and the paint( ) method for
your application, and invoke a repaint() call to re-render the
graphics, if necessary.
• At the end of the execution, the stop( ) method is invoked,
followed by the destroy( ) method to deallocate the applet’s
resources.
HTML tags for applets - 1
<APPLET specifies the beginning of the HTML applet code
// applet to display a message in a window // set font, and color and display
import java.awt.*; message on
import java.applet.*; // the screen at position 250,150
g.setFont(font);
public class hello extends Applet{
g.setColor(Color.blue);
public void init( ){
setBackground(Color.yellow);
// The message in the next line
}
is the one you will see
g.drawString("Hello,
world!",250,150);
}
}
Painting
a p p le t d o w n lo a d
serv er Y a llo w e d
c o n n e c t io n r e q u e s t a p p le t
f o r b id d e n
H ost X
c o n n e c t io n r e q u e s t
serv er Z
Summary
• An applet is a Java class
• Its code is downloaded from a web server
• It is run in the browser’s environment on the client host
• It is invoked by a browser when it scans a web page
and encounters a class specified with the APPLET tag
• For security reason, the execution of an applet is
normally subject to restrictions:
– applets cannot access files in the file system on the client
host
– Applets cannot make network connection exception to the
server host from which it originated
Proxy server
A proxy server can be used to circumvent the security
constraints.
Server host C lient ho st
H TTP se rve r brow ser
a p p le t d o w n lo a d
server Y
c o n n e c t io n r e q u e s t a p p le t
H ost X
c o n n e c t io n r e q u e s t
serv er Z