applets_basic introduction
DESCRIPTION
Java - An Introduction to AppletsTRANSCRIPT
Applets
Internet Tools and Java Programming
12/04/23 Applets 1
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
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
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
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
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
ILLUSTRATION
12/04/23 Applets 7
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
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
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
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
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
APPLET VIEWER
• Include the HTML code as comments• Documented with necessary type of HTML
statements
12/04/23 Applets 13
APPLICATION vs. APPLET
12/04/23 Applets 14
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
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
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
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
KEY POINTS
12/04/23 Applets 19
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
Thank You
12/04/23 Applets 21