CH 2 Java Applets
CH 2 Java Applets
CH 2 Java Applets
Java Applets
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 demand on
a server program and that runs in the context of a
web server process.
2
Introduction
Applet
Local Remote
applet applet
4
1. Local Applet:
◼ An Applet developed locally and stored in a
local system is known as a “local applet”.
◼ It doesn't need Internet connection.
5
2.Remote Applet
◼ A Remote Applet is developed by someone else
and stored on remote computer connected to the
internet.
◼ If the system is connected to the internet only
then remote applets can be downloaded for use.
6
Difference Between Local Applet and Remote Applet
◼ Key differences between Local applet and Remote applet.
Local Applet Remote Applet
There is no need to define the Applet's We need to define the Applet's URL in
URL in Local Applet. Remote Applet.
Local Applet is available on our Remote Applet is not available on our
computer. computer.
In order to use it or access it, we don't In order to use it or access it on our
need Internet Connection. computer, we need an Internet Connection.
7
Steps to Develop an Applet
8
Applet life cycle (1)
❖ Running state
❖ Idle state
◼ As displayed in the
on this diagram, Object
Applet
JApplet
15
How to Run an Applet?
16
Simple example of Applet by html file:
To execute the applet by html file, create an applet and compile it.
After that create an html file and place the applet code in html file.
Now click the html file.
//First.java <html>
import java.applet.Applet; <body>
import java.awt.Graphics; <applet code="First.class" width
public class First extends Applet ="300" height="300">
{
</applet>
public void paint(Graphics g)
{ </body>
g.drawString(“Hello </html>
World",150,150);
}
}
17
To execute the applet by applet viewer tool, create an
applet that contains applet tag in comment and compile
it. After that run it by: applet viewer First.java. Now Html
file is not required but it is for testing purpose only.
//First.java
import java.applet.Applet; ◼ To execute the applet by
import java.awt.Graphics; applet viewer tool, write
public class First extends Applet in command prompt:
{
public void paint(Graphics g){
◼ c:\>javac First.java
g.drawString(“Hello world",150,150); c:\>appletviewer First.java
} }
/*
<applet code="First.class" width="300
" height="300">
</applet>
*/ 18
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 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
19
Applet Security
For security reasons, applets that are loaded over
the network have several restrictions.
◼ an applet cannot ordinarily read or write files
20
Advanced Applets
◼ You can use threads in an applet.
◼ You can make socket calls in an applet, subject to the security
constraints.
applet download
server Y allowed
connection request applet
forbidden
Host X
connection request
server Z
21
Proxy server
A proxy server can be used to circumvent the security constraints.
applet download
server Y
connection request applet
Host X
connection request
server Z
22
How Applet Differ From Other Applications
◼ Applets don’t use the main() method, but when they are loaded,
automatically call certain methods (init, start, paint, stop,
destroy).
◼ They are restricted from using libraries from other languages like
such as C or C++.
23
Parameters Java Application Java Applet
Meaning • A Java Application also • The Java applet works on the client side,
known as application and runs on the browser and makes use
program is a type of of another application program so that
program that we can execute it.
independently executes
on the computer.
Requirement of • Its execution starts with • It does not require the use of any main()
main( ) method the main( ) method only. method. Java applet initializes through
The use of the main( ) is init( ) method.
mandatory.
Execution • It cannot run • It cannot start independently but
independently, but requires APIs for use (Example. APIs
requires JRE to run. like Web API).
Installation • We need to install the • Java applet does not need to be pre-
Java application first and installed.
obviously on the local
computer.
24
Parameters Java Application Java Applet
File access • It can easily access a file or • It cannot access the file or data
data available on a computer found on any local system or
system or device. computer.
Security • Java applications are pretty • Java applets are less reliable. So,
trusted, and thus, come with they need to be safe.
no security concerns.
25