0% found this document useful (0 votes)
8 views

JavaApplets

Java applets are programs that run within a browser session and are downloaded from a web server. They are subclasses of the java.Applet or javax.swing.JApplet classes and follow a specific life cycle with methods like init(), start(), stop(), and destroy(). Due to security restrictions, applets cannot access the client's file system or make network connections except to their originating server.

Uploaded by

beabzk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

JavaApplets

Java applets are programs that run within a browser session and are downloaded from a web server. They are subclasses of the java.Applet or javax.swing.JApplet classes and follow a specific life cycle with methods like init(), start(), stop(), and destroy(). Due to security restrictions, applets cannot access the client's file system or make network connections except to their originating server.

Uploaded by

beabzk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Java applets

25/02/08 1
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 a server
program, and it runs in the context of a web server
process.

25/02/08 2
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 applet program in the browser’s
execution context on the client host.

25/02/08 3
Applets, web page, client, server
server host
browser host
web server
browser
reqeust for
myWebPage.html

myWebPage.html
myWebPage.html HelloWorld.class
... request for
<applet code=HelloWorld.class</applet> HelloWorldclass
...
HelloWorld.class HelloWorld.class

25/02/08 4
Applet Execution - 1

◼ An applet program is a written as a subclass


of the java.Applet class or the
javax.swing.Japplet class.
◼ There is no main() method in an Applet.
◼ An applet uses AWT for graphics, or JApplet,
a subclass of javax.swing.

25/02/08 5
Applet Execution - 2
◼ Life Cycle of an Applet:
◼ init: This method is intended for whatever initialization
is needed for an applet.
◼ start: This method is automatically called after init
method. It is also called whenever user returns to the
page containing the applet after visiting other pages.
◼ stop: This method is automatically called whenever the
user moves away from the page containing applets.
This method can be used to stop an animation.
◼ destroy: This method is only called when the browser
shuts down normally.

◼ Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/

25/02/08 6
Applet Execution - 3

◼ The applet is running and rendered on the


web page.
◼ Every Applet needs to implement one of
more of the init(), the start( ) and the
paint( ) methods.
◼ At the end of the execution, the stop( )
method is invoked, followed by the
destroy( ) method to deallocate the
applet’s resources.

25/02/08 7
Applet Security

For security reasons, applets that are loaded over


the network have several restrictions.
◼ an applet cannot ordinarily read or write files

on the computer that it's executing on.


◼ an applet cannot make network connections

except to the host that it came from.

◼ Ref: http://java.sun.com/docs/books/tutorial/deployment/applet/index.html/

25/02/08 8
HTML tags for applets - 1
<APPLET
// the beginning of the HTML applet code
CODE="demoxx.class"
// the actual name of the applet (usually a 'class' file)
CODEBASE="demos/"
// the location of the applet (relative as here, or a full URL)
NAME=“SWE622"
// the name of the instance of the applet on this page
WIDTH="100"
// the physical width of the applet on the page
HEIGHT="50"
// the physical height of the applet on the page
ALIGN="Top"
// align the applet within its page space (top, bottom, center)

25/02/08 9
HTML tags for applets - 2
<APPLET CODE=“SWE622.class" CODEBASE="example/"
WIDTH=460 HEIGHT=160
NAME="buddy" >
<PARAM NAME="imageSource" VALUE="images/Beans">
<PARAM NAME="backgroundColor" VALUE="0xc0c0c0">
<PARAM NAME="endImage" VALUE=10>
</APPLET>

25/02/08 10
The HelloWorld Applet
<HTML> public void paint(Graphics g) {
<BODY> final int FONT_SIZE = 42;
<APPLET code=hello.class width=900 height=300> Font font = new Font("Serif",
</APPLET> Font.BOLD, FONT_SIZE);
</BODY> // set font, and color and display message
</HTML>
// on the screen at position 250,150
g.setFont(font);
// applet to display a message in a window
g.setColor(Color.blue);
import java.awt.*;
// The message in the next line is the one
import java.applet.*; // you will see
g.drawString("Hello,
public class hello extends Applet { world!",250,150);
public void init( ) {
} // end of paint()
setBackground(Color.yellow);
} // end of init()
} // end of hello

25/02/08 11
Advanced Applets
◼ You can use threads in an applet.
◼ You can make socket/RMI calls (hw_#3) in an applet, subject to the security
constraints.
Server host Client host
HTTP server browser

applet download
server Y allowed
connection request applet

forbidden
Host X
connection request
server Z

25/02/08 12
Proxy server
A proxy server (Y) can be used to circumvent the security
constraints.
Server host Client host
HTTP server browser

applet download
server Y
connection request applet

Host X
connection request
server Z

25/02/08 13
Summary
◼ An applet is a Java class
◼ Its code is downloaded from a web server
◼ It runs 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

25/02/08 14

You might also like