01/30/15 Distributed Computing, M. L. Liu 1
Java applets
M. L. Liu
01/30/15 Distributed Computing, M. L. Liu2
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.
01/30/15 Distributed Computing, M. L. Liu3
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.
< a p p l e t c o d e = H e l l o W o r l d . c l a s s < / a p p l e t >
...
...
H e l l o W o r l d . c l a s s
s e r v e r h o s t
w e b s e r v e r
m y W e b P a g e . h t m l
b r o w s e r h o s t
b r o w s e r
r e q e u s t f o r
m y W e b P a g e . h t m l
m y W e b P a g e . h t m l
r e q u e s t f o r
H e l l o W o r l d c l a s s
H e l l o W o r l d . c l a s s
H e l l o W o r l d . c l a s s
01/30/15 Distributed Computing, M. L. Liu4
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: you must override the
start method. Applet objects uses AWT for graphics.
JApplet uses SWING.
 It is a Grapics object that runs in a Thread object, so
every applet can perform graphics, and runs in
parallel to the browser process.
01/30/15 Distributed Computing, M. L. Liu5
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 destry( ) method to deallocate the applet’s
resources.
01/30/15 Distributed Computing, M. L. Liu6
Applet Security
http://java.sun.com/docs/books/tutorial/applet/overview/
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.
01/30/15 Distributed Computing, M. L. Liu7
HTML tags for applets - 1
<APPLET specifies the beginning of the HTML applet code
CODE="demoxx.class" is the actual name of the applet (usually a
'class' file)
CODEBASE="demos/" is the location of the applet (relative as here,
or a full URL)
NAME="smily" the name you want to give to this instance of the
applet on this page
WIDTH="100" the physical width of the applet on your page
HEIGHT="50" the physical height of the applet on your page
ALIGN="Top" where to align the applet within its page space (top,
bottom, center)
01/30/15 Distributed Computing, M. L. Liu8
HTML tags for applets - 2
<PARAM specifies a parameter that can be passed to the applet
NAME=“name1" the name known internally by the applet in order to
receive this parameter
VALUE="000000" the value you want to pass for this parameter
> end of this parameter
<PARAM specifies a parameter that can be passed to the applet (applet
specific)
NAME=“name2" the name known internally by the applet in order to
receive this parameter
VALUE="ffffff" the value you want to pass for this parameter
> end of this parameter
</APPLET> specifies the end of the HTML applet code
01/30/15 Distributed Computing, M. L. Liu9
The HelloWorld Applet
<HTML>
<BODY>
<APPLET code=hello.class width=900
height=300>
</APPLET>
</BODY>
</HTML>
// applet to display a message in a
window
import java.awt.*;
import java.applet.*;
public class hello extends Applet{
public void init( ){
setBackground(Color.yellow);
}
public void paint(Graphics g){
final int FONT_SIZE = 42;
Font font = new
Font("Serif", Font.BOLD,
FONT_SIZE);
// set font, and color and display
message on
// the screen at position 250,150
g.setFont(font);
g.setColor(Color.blue);
// The message in the next line is
the one you will see
g.drawString("Hello,
world!",250,150);
}
}
01/30/15 Distributed Computing, M. L. Liu10
Advanced Applets
 You can use threads in an applet.
 You can make socket calls in an applet, subject to the security constraints.
S e r v e r h o s t C l i e n t h o s t
H T T P s e r v e r b r o w s e r
a p p l e t
H o s t X
a p p le t d o w n lo a d
c o n n e c t io n r e q u e s t
c o n n e c t io n r e q u e s t
f o r b id d e n
a llo w e ds e r v e r Y
s e r v e r Z
01/30/15 Distributed Computing, M. L. Liu11
Proxy server
A proxy server can be used to circumvent the security constraints.
S e r v e r h o s t C l i e n t h o s t
H T T P s e r v e r b r o w s e r
a p p l e t
H o s t X
a p p le t d o w n lo a d
c o n n e c t io n r e q u e s t
s e r v e r Y
s e r v e r Z
c o n n e c t io n r e q u e s t
01/30/15 Distributed Computing, M. L. Liu12
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

Java applets

  • 1.
    01/30/15 Distributed Computing,M. L. Liu 1 Java applets M. L. Liu
  • 2.
    01/30/15 Distributed Computing,M. L. Liu2 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.
  • 3.
    01/30/15 Distributed Computing,M. L. Liu3 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. < a p p l e t c o d e = H e l l o W o r l d . c l a s s < / a p p l e t > ... ... H e l l o W o r l d . c l a s s s e r v e r h o s t w e b s e r v e r m y W e b P a g e . h t m l b r o w s e r h o s t b r o w s e r r e q e u s t f o r m y W e b P a g e . h t m l m y W e b P a g e . h t m l r e q u e s t f o r H e l l o W o r l d c l a s s H e l l o W o r l d . c l a s s H e l l o W o r l d . c l a s s
  • 4.
    01/30/15 Distributed Computing,M. L. Liu4 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: you must override the start method. Applet objects uses AWT for graphics. JApplet uses SWING.  It is a Grapics object that runs in a Thread object, so every applet can perform graphics, and runs in parallel to the browser process.
  • 5.
    01/30/15 Distributed Computing,M. L. Liu5 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 destry( ) method to deallocate the applet’s resources.
  • 6.
    01/30/15 Distributed Computing,M. L. Liu6 Applet Security http://java.sun.com/docs/books/tutorial/applet/overview/ 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.
  • 7.
    01/30/15 Distributed Computing,M. L. Liu7 HTML tags for applets - 1 <APPLET specifies the beginning of the HTML applet code CODE="demoxx.class" is the actual name of the applet (usually a 'class' file) CODEBASE="demos/" is the location of the applet (relative as here, or a full URL) NAME="smily" the name you want to give to this instance of the applet on this page WIDTH="100" the physical width of the applet on your page HEIGHT="50" the physical height of the applet on your page ALIGN="Top" where to align the applet within its page space (top, bottom, center)
  • 8.
    01/30/15 Distributed Computing,M. L. Liu8 HTML tags for applets - 2 <PARAM specifies a parameter that can be passed to the applet NAME=“name1" the name known internally by the applet in order to receive this parameter VALUE="000000" the value you want to pass for this parameter > end of this parameter <PARAM specifies a parameter that can be passed to the applet (applet specific) NAME=“name2" the name known internally by the applet in order to receive this parameter VALUE="ffffff" the value you want to pass for this parameter > end of this parameter </APPLET> specifies the end of the HTML applet code
  • 9.
    01/30/15 Distributed Computing,M. L. Liu9 The HelloWorld Applet <HTML> <BODY> <APPLET code=hello.class width=900 height=300> </APPLET> </BODY> </HTML> // applet to display a message in a window import java.awt.*; import java.applet.*; public class hello extends Applet{ public void init( ){ setBackground(Color.yellow); } public void paint(Graphics g){ final int FONT_SIZE = 42; Font font = new Font("Serif", Font.BOLD, FONT_SIZE); // set font, and color and display message on // the screen at position 250,150 g.setFont(font); g.setColor(Color.blue); // The message in the next line is the one you will see g.drawString("Hello, world!",250,150); } }
  • 10.
    01/30/15 Distributed Computing,M. L. Liu10 Advanced Applets  You can use threads in an applet.  You can make socket calls in an applet, subject to the security constraints. S e r v e r h o s t C l i e n t h o s t H T T P s e r v e r b r o w s e r a p p l e t H o s t X a p p le t d o w n lo a d c o n n e c t io n r e q u e s t c o n n e c t io n r e q u e s t f o r b id d e n a llo w e ds e r v e r Y s e r v e r Z
  • 11.
    01/30/15 Distributed Computing,M. L. Liu11 Proxy server A proxy server can be used to circumvent the security constraints. S e r v e r h o s t C l i e n t h o s t H T T P s e r v e r b r o w s e r a p p l e t H o s t X a p p le t d o w n lo a d c o n n e c t io n r e q u e s t s e r v e r Y s e r v e r Z c o n n e c t io n r e q u e s t
  • 12.
    01/30/15 Distributed Computing,M. L. Liu12 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