0% found this document useful (0 votes)
6 views35 pages

Chapter 2 Java Applet

Chapter 2 provides an overview of Java applets, which are small Java programs that run in web browsers to create dynamic content. It details the lifecycle of applets, the methods invoked by browsers, and the differences between applets and standalone Java applications, emphasizing security restrictions on applets. The chapter also discusses the use of the <applet> tag in HTML and common applications for both applets and Java applications.

Uploaded by

gadisekelbesa07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

Chapter 2 Java Applet

Chapter 2 provides an overview of Java applets, which are small Java programs that run in web browsers to create dynamic content. It details the lifecycle of applets, the methods invoked by browsers, and the differences between applets and standalone Java applications, emphasizing security restrictions on applets. The chapter also discusses the use of the <applet> tag in HTML and common applications for both applets and Java applications.

Uploaded by

gadisekelbesa07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 2: Java Applet

2.1. Overview of Java Applets

2.2. Java Applets Vs Java Application

1
Overview of Java Applets

• An applet is a special Java program that runs in a Web browser and


embedded in the webpage to generate a dynamic content.
• It works in the client side because it runs inside the browsers.
• The term applet is derived from application, as if to imply that an applet is a
small application.
• Applets are truly platform-independent program. Because applets are a part
of a web page, they can be accessed by any Web browser using any operating
system on any device, and therefore can be executed on many different
platform and devices.
2
Lifecycle of Java Applet
• When a user views a Web page that contains an applet, the following
sequence of events occurs regarding the life cycle of the applet:
1. The Web browser downloads the necessary bytecode and JAR file from the
Web server where the code is located. (This Web server is referred to as the
code base.)
2. The browser creates an instance of the Applet class, invoking the default
constructor.
3. The applet is displayed in the Web page, with the location and size of the
applet determined by the HTML.
3
Cont…
4. The browser invokes the init() method on the applet.
5. The browser invokes the start() method on the applet.
6. The browser invokes the paint() method on the applet.
7. The applet is now live and running within the Web page.
8. The browser calls paint() whenever the applet needs to repaint itself.
9. The browser invokes the stop() method when the user leaves the Web page or
the applet is about to be destroyed.
10. The browser invokes the destroy() method just before destroying the applet.

4
The five methods that the browser invokes on the applet:

1. public void init(): The first method invoked on the applet when it is initially
instantiated. This is your chance to perform any initialization, such
as locating resources or preparing event handlers.
2. public void start(): Invoked by the browser to inform the applet that it
should start executing. The start() method is called right after the init()
method, and is also called when the page is revisited. This is a good time
to start any threads or other tasks like displaying animation or playing
sound. .

5
Cont…
3. public void stop(): Invoked by the Web browser to inform the applet that it
should stop executing. The stop() method is called right before the
destroy() method is invoked, and also when a user leaves the Web page.
Typically, anything you started in the start() method is stopped in the
stop() method.
4. public void destroy(): Invoked by the Web browser to inform the applet that it
is about to be destroyed (in other words, garbage collected). Typically, any
resources allocated in the init() method are freed in the
destroy() method.
6
Cont…
5. public void paint(Graphics g): Invoked immediately after the start()
method, and also any time the applet needs to repaint itself in the browser.
The paint() method is actually inherited from the java.awt.
• Container class (the grandparent class of Applet), and is a feature of all
containers. The java.awt.Graphics parameter is the graphics context,
representing the portion of the screen on which your applet is allowed to
paint.

7
(a) shows how the browser calls these methods, and (b) illustrates the flow of control of an
applet using a statechart diagram. 8
The Applet Class

• The Applet class provides the essential framework that enables applets
to be run from a Web browser.
• The Applet class provides a common interface so that a Web browser
can communicate with the applet.
• While every Java application has a main method that is executed, when
the application starts, applets do not have a main method. Instead they
depend on the browser to call the methods in the Applet class.
• Every applet is a subclass of java.applet.Applet
9
Cont…
• Applet class is extends java.awt.Panel. This means that an applet is a
panel, which is a java.awt.Container.
• Therefore, an applet can have components added to it just like any
container, as well as have a layout manager assigned to it, and you can
even nest panels within an applet to create the GUI you want.

10
General form of applet class

public class MyApplet extends java.applet.Applet {


...
/** The no-arg constructor is called by the browser when the Web
* page containing this applet is initially loaded, or reloaded
*/
public MyApplet() {
...
}
// init() Called by the browser after the applet is loaded
public void init() {
...
} continued to the next page… 11
Cont…
// start() Called by the browser after the init() method,
or every time the Web page is visited
public void start() {
...
}
/* paint(Graphics g)Called by the browser when the applet
output must be re-drawn or re-painted */
public void paint(Graphics g){
...
}
continued to the next page… 12
Cont…

/* stop() Called by the browser when the page containing


this applet becomes inactive (minimized, etc) */
public void stop() {
...
}
/** destroy() Called by the browser when the Web browser
exits */
public void destroy() {
...
}
/** Other methods if necessary... */
}

13
The <applet> Tag

The <applet> tag: is used to embed an applet within an HTML page. the applet
tag has three required attributes: code, width, and height. The following is a list of
the other applet tags attributes, each of which is optional:
• codebase: The location where the browser can find the bytecode for the
Applet. The code base represents a URL (relative to where the HTML
document is located) where the necessary applet bytecode is located.
• archive: The name of a JAR file (or files) that contains any additional files
required by the applet. Use a comma to separate multiple filenames.

14
Cont…
• All .class files in the archives are loaded by the JVM’s class loader,
making the classes available to the applet.
• name: The name of this instance of the applet. You can create multiple
applets on a Web page, and they can locate each other by using this
name attribute and the AppletContext class.
• align: A standard HTML attribute that specifies the alignment of the
applet relative to other items around it on the Web page. The possible
values are left, right, top, bottom, middle, texttop, absmiddle, absbottom,
and baseline. 15
Cont…
• vspace: A standard HTML attribute that specifies the number of pixels to
appear above and below the applet.
• hspace: A standard HTML attribute that specifies the number of pixels to
appear to the left and right of the applet.
• alt: A standard HTML attribute that allows you to specify text that
appears if the browser understands the <applet> tag but cannot run the
applet.
Note: If a Web browser does not understand the <applet> tag, it will ignore
the tag and display any HTML that appears within the opening and closing
16
<applet> tags.
Example Cont…
<html>
<body>
<h2>Enter your name and click the button.</h2>
<applet code=”HelloWorldApplet2”
width=”200”
height=”75”
name=”HelloWorld”
align=”center”
vspace=”15”
alt=”The HelloWorld2 applet”>
<param name=”greeting” value=”Merry Christmas”>
<param name=”buttonLabel” value=”Click here”>
</applet>
</body>
</html>
17
Introduction to HTML

• HTML stands for HyperText Markup Language and is the language of the
Internet. A Web page is an HTML document, and HTML looks nothing like
the page you actually view.
• The Web browser takes the HTML and marks it up (thus the term “markup”
language).
• An HTML document is a text file saved with either a .htm or .html
extension. The root tag of an HTML document is <html>, and the <html>
tag can nest the optional <head> and <body> tags.
18
Cont…

• Structure of HTML
<html>

<head>
</head>
<body>
</body>
</html>

19
Cont…

• The <head> tag can contain the <title> tag, which denotes the text to appear in
the title bar of the browser’s window. Other tags that typically appear within
<head> include: <meta>.
• Used to define variables like the search keywords, content type, description of the
page, and so on.
• <style>: Used for defining styles on the page (fonts, colors, cursors, and so on).
• <script>: Used for defining programming functions in languages like javascript.

20
Cont…
• For example, the following Web page displays Welcome in the title bar of the
browser and defines keywords that are used by search engines to determine the
content of the page:
<header>
<title>Welcome</title>
<meta name=”keywords” content=”java, training, courseware,books”>
</header>
• Note: search engines rank their search results on the <meta> keywords. If
someone searches for “java training,” ideally this Web page will appear early on
in the search results. 21
Cont…
• The <body> tag contains the content of the Web page. Common tags in the
body of a page include: <p>. The paragraph tag.
• <hx>: The heading tag. Possible values of x and 1–6. For example, <h1> is the
heading 1 tag, which creates a large heading. <h6> is the smallest heading.
• <center>: For centering items.
• <a href=”url”>: The anchor tag; it creates a hyperlink to some other URL.
• <applet>: Embeds an applet in the HTML document.
• <table>: For creating a table. Nest <tr> within <table> to create a table row,
and nest <td> within <tr> to create columns for the table data.
22
Example Cont…
<body>
<h2>The Hello Swing Applet</h2>
<p>This applet demonstrates using the JApplet class.</p>
<center>
<table border=”1”>
<tr><td>
<applet code=”HelloSwingApplet” width=”250” height=”250”>
</applet>
</tr></td>
</table>
</center>
23
</body>
An applet vs a standalone Java application

• Applets: are small programs that can be embedded into a web page. The
applets are used to make the website more dynamic and entertaining. A
web browser that is compatible with Java can easily run applets.
• Java applications: are types of stand-alone applications that run directly
on the underlying operating system with the help of a virtual machine.
These perform various general operations for their users and do not
require any APIs or browsers enabled by Java.

24
Some of the key differences between Java applications and Java
applets:

1. Execution: Java applications always begin with the main() method,


while in the case of an applet, initialization takes through the init()
method. There is no need to invoke the main() method.
2. Access: Java application programs have full access to the local file
system and network. Applets don’t have local disk and network access.

25
Cont…

3. Requirements: Java applications do not require any web browser for


their execution. Execution can be easily done via the local system. On the
other hand, applets require a web browser (Java based) to use.
4. Security: Java applications can access the system’s data and resources
without any security limitations. Applets are executed in a more restricted
environment with tighter security. They can only use services that are
exclusive to their browser.

26
Generally:
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will
(typically) not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the
applet is downloaded to the user’s machine.
• A user must have a JVM on his or her machine. The JVM can be either a
plug-in of the Web browser or a separate runtime environment.
27
Cont…
• The JVM on the user’s machine creates an instance of the applet class
and invokes various methods during the applet’s lifetime.
• Applets have strict security rules that are enforced by the Web browser.
The security of an applet is often referred to as sandbox security,
comparing the applet to a child playing in a sandbox with various rules
that must be followed.
• Other classes that the applet needs can be downloaded in a single Java
Archive (JAR) file.
28
Sandbox Security
• Applets run in a Web browser restricted to a set of security policies
referred to as sandbox security.
• The purpose of the sandbox security model is to ensure that the applet
you are downloading and executing is not going to do terrible things to
your computer. This is especially important in today’s Internet world of
viruses and other undesirable side effects of software applications.
Applets that are downloaded and executed in a Web browser must
adhere to the following rules:
o An applet cannot access any files on a user’s operating system. 29
o An applet cannot access any files on a user’s operating system.
o An applet can only create network connections to the applet’s code
base, and cannot connect to other network addresses.
o An applet cannot execute a program on the user’s machine.
o An applet cannot access system information or use system dialog
boxes such the Open File or Print dialog boxes.

30
Note:
• An applet can be granted permission to leave the sandbox and perform an
otherwise restricted operation. For example, you can grant an applet
permission to access files on your local hard drive. Of course, you will want
to make sure you trust the source of the applet before granting such
permission.
• An applet can also be signed, which involves creating a security certificate.
This is the typical way to create an applet that needs to perform tasks outside
the sandbox because it provides the user of the applet with some assurance as
to the source of the applet, letting the user decide whom he or she trusts.
31
Common uses of Java Applets:
• Applets can be used to add small, interactive components or enhancements
to a webpage such as buttons, scrolling text, or stock tickers.
• They can also be used to display larger programs like word processors or
games.
Common uses for Java Applications:
• Android mobile app development: Java is the official language for
Android mobile app development. In fact, the Android operating system
itself is written in Java.
32
Common uses for Java Applications:
• Desktop applications: Java has been used to create desktop applications
since its inception. AWT, Swing, and JavaFX are Java libraries that give
desktop application developers pre-built components like buttons, menus,
and form fields that they can use to build full-featured desktop applications.
• Web applications: Java is still very popular for creating back-end web
applications, which run on a web server. Web developers use Java
technologies like Struts, Servlets, or JSP to create all types of full-featured
web applications.
33
Common uses for Java Applications:
• Cloud-based applications: Java is also heavily used in cloud-based
applications. Many companies use it to develop SaaS, IaaS, and PaaS
services in the cloud.

34
35

You might also like