CH 2 Java Applets

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Chapter 2

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

◼ An Applet is the special type of Java program that is run


on web browser.
◼ An applet class does not have main method and it extends
the java.applet.Applet class. An applet program runs
through applet viewer.
◼ An applet is a special kind of Java program that is
designed to be transmitted over the Internet and
automatically executed by a Java-compatible web
browser.
3
Types of Applet

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.

It is written on our own and then It was written by another developer.


embedded into the web pages.
We don't need to download it. It is available on a remote computer, so we
need to download it to our system.

7
Steps to Develop an Applet

1. Building an applet code(.java file)

2. Creating an executable file.(*.class file)

3. Designing a web page using HTML tags.

4. Preparing <APPLET> tag.

5. Creating HTML file.

6. Testing the applet code.

8
Applet life cycle (1)

◼ When an applet is loaded it


undergoes a series of
changes in its states.

◼ The applet states include


❖ Born or Initialization
state

❖ Running state

❖ Idle state

❖ Dead or destroyed state


9
Applet life cycle_Initialization State
➢ An Applet when loaded it is on its initialization state.
➢ At this state init() method of class Applet is called.
➢ This is the new born state of an applet.
➢ At this stage,
▪ Objects can be created those are needed by the applet.
▪ Initial vales are specified.
▪ Images are loaded.
▪ Font colors are set up for Applet.
Public void init ()
{
--
}
Applet life cycle_Running State
 Applet enters the running state when the
system calls the start() method of Applet class
 This occurs automatically after the applet is
initialized.
 Start() method can be called more than one
time.
Public void start()
{
--
---
}
Applet life cycle_Display State
 Applet moves to the display state when it has
to perform some output operations on the
screen.
 This happens when applet is in running state.
 The paint() method perform this task.
public void paint(Graphics g)
{
--
---
}
Applet life cycle_Idle State
 An Applet becomes idle when it is stopped from
running.
 Stopping occurs automatically when someone
leave the page containing the running applet.
 An applet will send to idle state by explicitly
calling stop() method.
Public void stop()
{
--
---
}
Applet life cycle_Dead State

 An applet is said to be dead when it is


removed from memory.
 This happens automatically by invoking
destroy() method ,when someone quit the
browser.
public void destroy()
{
--
---
}
Hierarchy of Applet

◼ As displayed in the
on this diagram, Object

Applet class extends


Component
Panel. Panel class
extends Container Container
which is the subclass
of Component. Panel

Applet

JApplet

15
How to Run an Applet?

There are two ways to run an applet


◼ By html file.
◼ By applet Viewer tool .

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

on the computer that it's executing on.


◼ an applet cannot make network connections

except to the host that it came from.

20
Advanced Applets
◼ You can use threads in an applet.
◼ You can make socket calls 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

21
Proxy server
A proxy server 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

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 embedded inside a web page and executed in browsers.

◼ Takes input through Graphical User Input ( GUI ).

◼ They cannot read from or write to the files on local computer.

◼ They cannot run any programs from the local computer.

◼ 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

Connectivity • It is possible to establish • It cannot establish connection to


with server connections with other other servers.
servers.

Operation • It performs read and write • It cannot run the applications on


tasks on a variety of files any local computer.
located on a local computer.

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

You might also like