Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Applications
Java Applets
• Applet is a special type of program that is embedded in the webpage
to generate the dynamic content. It runs inside the browser and
works at client side.
• Applets are small Internet-based program written in Java, a
programming language for the Web.
• The applet is usually embedded in an HTML page on a Web site and
can be executed from within a browser.
JAVA Applets
The applet begins with two import statements. The first import statement is
for the Abstract Window Toolkit (AWT) classes. Applets interact with the user
through the AWT and not through the console-based I/O classes. An applet
must be a subclass of the java.applet.Applet class. The Applet class provides
the standard interface between the applet and the browser environment.
Swing provides a special subclass of the Applet class called
javax.swing.JApplet. The JApplet class should be used for all applets that use
Swing components to construct their graphical user interfaces (GUIs). The
browser's Java Plug-in software manages the lifecycle of an Applet. The
applet in Java can appear in a frame of the web page, a new application
window, Sun's AppletViewer, or a stand-alone tool for testing them. They
were introduced in the first version of the Java language, which was
introduced in the year 1995.
Advantages of Java Applets
• They are very secure.
• It works at client side so less response time.
• Applets can be executed by browsers running under different
platforms.
One disadvantage of Applets is that plugins are required at the client
browser for executing applets.
Life Cycle of Java Applets
An applet undergoes various stages between its creation of objects and object
removal as the work of the applet will get done. This cycle is known as Applet life
cycle, where each state is signified by methods. In the life of execution, the applet
exists (lives) in one of these 5 states. All of these methods have a name and they
are called as callback methods. These methods are named so because they are
called automatically by the browser when required for smooth execution of the
applet. Here, programmers write the above-mentioned methods with some code
but never calls. Following are the methods for a full applet cycle.
• init() method
• start() method
• paint() method
• stop() method
• destroy() method
Life Cycle of Java Applets
• init − This method is intended for whatever initialization is needed for your
applet. It is called after the param tags inside the applet tag have been processed.
• start − This method is automatically called after the browser calls the init
method. It is also called whenever the user returns to the page containing the
applet after having gone off to other pages.
• stop − This method is automatically called when the user moves off the page on
which the applet sits. It can, therefore, be called repeatedly in the same applet.
• destroy − This method is only called when the browser shuts down normally.
Because applets are meant to live on an HTML page, you should not normally
leave resources behind after a user leaves the page that contains the applet.
• paint − 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.
How to run an Applet?
• These import statements bring the classes into the scope of our applet class −
java.applet.Applet
java.awt.Graphics
• Without those import statements, the Java compiler would not recognize the classes Applet and
Graphics, which the applet class refers to.
Invoking an Applet
An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer
or Java-enabled browser.
The <applet> tag is the basis for embedding an applet in an HTML file. Following is an example that invokes the
"Hello, World" applet −
<html>
<title>The Hello, World Applet</title>
<head>
<TITLE> A Simple Program </TITLE>
</head>
<body>
Here is the output of my program:
<applet code="HelloWorld.class" width="150" height="25">
</applet>
</body>
</html>
Simple example of Applet by appletviewer tool
To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by:
appletviewer First.java. Now Html file is not required but it is for testing purpose only.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
Execute the applet by appletviewer
tool
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac First.java
c:\>appletviewer First.java
Param Tag
The <param> tag is a sub tag of the <applet> tag.
The <param> tag contains two attributes: name and value which are used to specify the name of the parameter and the value of the
parameter respectively.
For example, the param tags for passing name and age parameters looks as shown below:
Now, these two parameters can be accessed in the applet program using the getParameter() method of the Applet class.
getParameter() Method
<message>
<text>Hello, world!</text>
</message>
<Employee>
<FirstName> Ambrish </FirstName>
<MiddleName> Kumar </MiddleName>
<LastName> singh </LastName>
<Age> 25 </Age>
<EMPID> A24 <EMPID>
</Employee>
XML Declaration
• XML-declaration is optional in XML 1.0, mandatory in 1.1.
• Encoding: It defines the character encoding used in the document.
UTF-8 is the default encoding used.
• Standalone: is the xml-document linked to external markup
declaration
• – yes: no external markup declarations
• – no: can have external markup
• – default: "no"
XML Element
• An element name can contain any alphanumeric characters. The only
punctuation mark allowed in names are the hyphen (-), under-score
(_) and period (.).
• Element names must start with a letter or underscore
• Names are case sensitive.
• For example, Address, address, and ADDRESS are different names.
• Start and end tags of an element must be identical.
• Element names cannot contain spaces
XML Attributes
• Xml attributes provide additional information about elements.
• An attribute is name value pair contained in the element’s start tag,
where in names are separated from values by assignment operator.