applets_basic introduction

21
Applets Internet Tools and Java Programming 03/07/22 Applets 1

Upload: charulatha-jain

Post on 02-Jun-2015

878 views

Category:

Education


0 download

DESCRIPTION

Java - An Introduction to Applets

TRANSCRIPT

Page 1: Applets_Basic Introduction

Applets

Internet Tools and Java Programming

12/04/23 Applets 1

Page 2: Applets_Basic Introduction

What are Applets?

• ”Applets are small applications that are accessed on an

Internet Server, transported over the Internet,

automatically installed,and run as part of a Web

document” ,

– (Schildt , Herbert ”Java 2: The complete Reference,2001)

12/04/23 Applets 2

Page 3: Applets_Basic Introduction

What are Applets?

• An applet is a program written in the Java programming language

that can be included in an HTML page, much in the same way an

image is included in a page. When you use a Java technology-

enabled browser to view a page that contains an applet, the

applet's code is transferred to your system and executed by the

browser's Java Virtual Machine (JVM)

– (http://java.sun.com/applets/)

12/04/23 Applets 3

Page 4: Applets_Basic Introduction

Applet

• Applet is a Java program executed by a browser

• The position of applets in software world is they occupy the

client-side position in Web communication

• On the server-side, another Java program comes, Servlets

• Applets on client-side and servlets on server-side makes Java

a truly "Internet-based language“

– http://way2java.com/applets/applets-vs-applications/

12/04/23 Applets 4

Page 5: Applets_Basic Introduction

Life cycle of an Applet

• init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag.

• 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. You can use this method to stop an animation.

• destroy: This method is only called when the browser shuts down normally

Thus, the applet can be initialized once and only once, started and stopped one or more times in its life, and destroyed once and only once.

12/04/23 Applets 5

Page 6: Applets_Basic Introduction

Program Execution Flow

12/04/23 Applets 6

To execute applets, the

browsers come with JRE

The browsers with Java

loaded are known as Java

enabled browsers

Page 7: Applets_Basic Introduction

ILLUSTRATION

12/04/23 Applets 7

Page 8: Applets_Basic Introduction

IMPORT STATEMENTS

• Applets interact with user through AWT • Abstract Window Toolkit– Contains support for a window-based graphical

interface

• Applet Package– Contains the class Applet– Every applet created must be its subclass

12/04/23 Applets 8

Page 9: Applets_Basic Introduction

CLASS, MEMBER FUNCTIONS

• Simple Applet is declared as public. Why?

– It is accessed by the code outside the program

• paint()

– Defined by AWT

– Overridden by the applet

12/04/23 Applets 9

Page 10: Applets_Basic Introduction

paint() and drawString()

• Parameter – Graphic

– Describes the graphics environment in which the

applet is running

• drawString()

– Member of Graphics class

– Outputs the string at a specified X,Y location

12/04/23 Applets 10

Page 11: Applets_Basic Introduction

RUNNING AN APPLET

• Executing an applet with a Java-compatible Web browser

• Using an applet viewer– Executes in a window– Fastest & Easiest way

12/04/23 Applets 11

Page 12: Applets_Basic Introduction

Web browser

• Write a short HTML• Dimensions of display area• Execute your browser and then load this file

12/04/23 Applets 12

HTML file

Page 13: Applets_Basic Introduction

APPLET VIEWER

• Include the HTML code as comments• Documented with necessary type of HTML

statements

12/04/23 Applets 13

Page 14: Applets_Basic Introduction

APPLICATION vs. APPLET

12/04/23 Applets 14

Page 15: Applets_Basic Introduction

ADVANTAGES OF APPLETS

• Deployment of applets is easy in a Web browser and does not require any installation procedure in real time programming

• Writing and displaying (just opening in a browser) graphics and animations is easier than applications

• In GUI development, constructor, size of frame, window closing code etc. are not required (but are required in applications)

12/04/23 Applets 15

Page 16: Applets_Basic Introduction

RESTRICTIONS• Applets require a compiler to compile and also a browser

to execute.• In realtime environment, the bytecode of applet is to be

downloaded from the server to the client machine• Applets are treated as untrusted and for this reason they

are not allowed, as a security measure, to access any system resources like file system etc. available on the client system

• Code can be developed to communicate between applets using AppletContext

12/04/23 Applets 16

Page 17: Applets_Basic Introduction

What Applet can't do – Security Limitations

• Applets are treated as untrusted because they are developed by somebody and placed on some unknown Web server

• When downloaded, they may harm the system resources or steal passwords and valuable information.

• As applets are untrusted, the browsers come with many security restrictions. Security policies are browser dependent.

• Browser does not allow the applet to access any of the system resources.

12/04/23 Applets 17

Page 18: Applets_Basic Introduction

LIMITATIONS

• Applets are not permitted to use any system resources like file system as they are un trusted and can inject virus into the system

• Applets cannot read from or write to hard disk files.

• The JRE throws SecurityException if the applet violates the browser restrictions

12/04/23 Applets 18

Page 19: Applets_Basic Introduction

KEY POINTS

12/04/23 Applets 19

Page 20: Applets_Basic Introduction

REFERENCES

• “Java : The Complete Reference”, Schildt, Herbert, TMH, 7th Edition, 2007

• http://way2java.com/applets/applets-vs-applications

• http://java.sun.com

12/04/23 Applets 20

Page 21: Applets_Basic Introduction

Thank You

12/04/23 Applets 21