Applets Programming: Enabling Application Delivery Via The Web
Applets Programming: Enabling Application Delivery Via The Web
2
Applet: Making Web Interactive and
Application Delivery Media
1 2 3 4 5
APPLET hello.class Create Accessing The browser
Development AT SUN’S Applet from creates
“hello.java” WEB tag in Your Organisation a new
AT SERVER HTML window and
SUN.COM document a new thread
and
then runs the
code
Hello Java
<app=
“Hello”> The Internet
Hello
3
How Applets Differ from
Applications
Although both the Applets and stand-alone applications
are Java programs, there are certain restrictions are
imposed on Applets due to security concerns:
Applets don’t use the main() method, but when they are
load, automatically call certain methods (init, start, paint,
stop, destroy).
They are embedded inside a web page and executed in
browsers.
They cannot read from or write to the files on local
computer.
They cannot communicate with other servers on the
network.
They cannot run any programs from the local computer.
They are restricted from using libraries from other
languages.
4
Building Applet Code: An Example
//HelloWorldApplet.java
import java.applet.Applet;
import java.awt.*;
5
Embedding Applet in Web Page
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Java Applet on the Web!</h1>
<APPLET CODE="HelloWorldApplet.class" width=500 height=400>
</APPLET>
</body>
</HTML>
6
Accessing Web page (runs
Applet)
7
Applet Life Cycle
Every applet inherits a set of default behaviours from the Applet class. As a
result, when an applet is loaded, it undergoes a series of changes in its state.
The applet states include:
8
Applet States
Initialisation – invokes init() – only once
Invoked when applet is first loaded.
Running – invokes start() – more than once
For the first time, it is called automatically by the system after
init() method execution.
It is also invoked when applet moves from idle/stop() state to
active state. For example, when we return back to the Web
page after temporary visiting other pages.
Display – invokes paint() - more than once
It happens immediately after the applet enters into the running
state. It is responsible for displaying output.
Idle – invokes stop() - more than once
It is invoked when the applet is stopped from running. For
example, it occurs when we leave a web page.
Dead/Destroyed State – invokes destroy() - only once
This occurs automatically by invoking destroy() method when
we quit the browser.
9
Applet Life Cycle Diagram
init()
Begin Born
stop()
start()
Running Idle
destroy()
paint() start()
Dead End
10
Passing Parameters to Applet
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET
CODE="HelloAppletMsg.class" width=500 height=400>
<PARAM NAME="Greetings" VALUE="Hello Friend, How are you?">
</APPLET>
</body>
</HTML>
11
Applet Program Accepting
Parameters
//HelloAppletMsg.java
import java.applet.Applet;
import java.awt.*;
String msg;
13
What happen if we don’t pass
parameter?
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Hi, This is My First Communicating Applet on the Web!</h1>
<APPLET
CODE="HelloAppletMsg.class" width=500 height=400>
</APPLET>
</body>
</HTML>
14
getParameter() returns null. Some
default value may be used.
15
Displaying Numeric Values
//SumNums.java
import java.applet.Applet;
import java.awt.*;
16
SunNums.html
<HTML>
<HEAD>
<TITLE>
Hello World Applet
</TITLE>
</HEAD>
<body>
<h1>Sum of Numbers</h1>
<APPLET CODE="SumNums.class" width=500 height=400>
</APPLET>
</body>
</HTML>
17
Applet – Sum Numbers
18
Interactive Applets
Applets work in a graphical environment.
Therefore, applets treats inputs as text
strings.
We need to create an area on the screen in
which user can type and edit input items.
We can do this using TextField class of the
applet package.
When data is entered, an event is
generated. This can be used to refresh the
applet output based on input values.
19
Interactive Applet Program..(cont)
//SumNumsInteractive..java
import java.applet.Applet;
import java.awt.*;
20
Interactive Applet Program.
sum = num1 + num2;
String str = "THE SUM IS: "+String.valueOf(sum);
g.drawString (str,100, 125);
}
public boolean action(Event ev, Object obj)
{
repaint();
return true;
}
}
21
Interactive Applet Execution
22
UserIn.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
txt1.setText("0");
txt2.setText("0");
txt3.setText("0");
high=new Button("highest");
high.addActionListener(this);
add(txt1);
add(txt2);
add(txt3);
add(high);
}
23
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s3;
try
{
s1=txt1.getText();
s2=txt2.getText();
s3=txt3.getText();
x=Integer.parseInt(s1);
y=Integer.parseInt(s2);
z=Integer.parseInt(s3);
}
catch(Exception e){}
24
UserIn.html
<html>
<body>
<applet code=UserIn.class
width= 400
height=400>
</applet>
</body>
</html>
25
26
Exercise
What are applets?
Differentiate between applet and application
program.
What are local and remote applets?
How do applets differ from applications?
Diagramatically explain Life cycle of applet
How to design applets?
How to execute applets?
How to provide interactive inputs?
27