Sei sulla pagina 1di 35

Java Applets in Web

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?

• There are two ways to run an applet


• By html file.
• By appletViewer tool (for testing purpose).
A "Hello,World" Applet
import java.applet.*;
import java.awt.*;

public class HelloWorld extends Applet


{
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}

• 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{

public void paint(Graphics g){


g.drawString("welcome to applet",150,150);
}

}
/*
<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:

<param name=”name” value=”Ramesh” />


<param name=”age” value=”25″ />

Now, these two parameters can be accessed in the applet program using the getParameter() method of the Applet class.
getParameter() Method

The getParameter() method of the Applet class can be used to retrieve


the parameters passed from the HTML page. The syntax
of getParameter() method is as follows:
String getParameter(String param-name)
Sample program
• Let’s look at a sample program which demonstrates the <param>
HTML tag and the getParameter() method:
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
*/
Output
XML (Extensible Markup Language)
• XML stands for Extensible Markup Language.
• XML tags identify the data and are used to store and organize the
data, rather than specifying how to display it like HTML tags, which
are used to display the data.
• XML is not going to replace HTML in the near future, but it introduces
new possibilities by adopting many successful features of HTML.
Characteristics of XML
There are three important characteristics of XML that make it useful in
a variety of systems and solutions −
• XML is extensible − XML allows you to create your own self-
descriptive tags, or language, that suits your application.
• XML carries the data, does not present it − XML allows you to store
the data irrespective of how it will be presented.
• XML is a public standard − XML was developed by an organization
called the World Wide Web Consortium (W3C) and is available as an
open standard.
What is Markup?

• XML is a markup language that defines set of rules for encoding


documents in a format that is both human-readable and machine-
readable. So what exactly is a markup language? Markup is
information added to a document that enhances its meaning in
certain ways, in that it identifies the parts and how they relate to
each other. More specifically, a markup language is a set of symbols
that can be placed in the text of a document to demarcate and label
the parts of that document.
Example
Following example shows how XML markup looks, when embedded in a piece of text −

<message>
<text>Hello, world!</text>
</message>

This snippet includes the markup symbols, or the tags such as


<message>...</message> and <text>... </text>.
The tags <message> and </message> mark the start and the end of the XML code
fragment.
The tags <text> and </text> surround the text Hello, world!.
Is XML a Programming Language?

A programming language consists of grammar rules and its own


vocabulary which is used to create computer programs. These
programs instruct the computer to perform specific tasks. XML does
not qualify to be a programming language as it does not perform any
computation or algorithms. It is usually stored in a simple text file and
is processed by special software that is capable of interpreting XML.
Syntax of XML
The following file is called "parts.xml".
<?xml version="1.0“ Encoding=“utf-8”?> //prolog
<patient> //Element
<name >
<first>Maggie</first>
<Middle> Dee<Middle>
</name>
</patient>
An XML element is everything from (including) the element's start tag to
(including) the element's end tag.
Syntax of XML
There are three ways to write illustrate element patient.
1. A tag with one attribute
<patient name=“Maggie”> </patient> //here name is attribute
2. A tag with one nested tag.
<patient>
<name >Maggie</name>
</patient>
3. A tag with one nested tag, which contains 3 nested tag.
<patient>
<name >
<first>Maggie</first>
<Middle> Dee</Middle>
</name>
</patient>
Where to use?
A short list of XML usage says it all −
• XML can separate data from HTML
• XML is used to exchange data
• XML can be used to share data
• XML can be used to store data
• XML can make your data more useful
XML Editors
• XML documents simple written by using simple text editors. This could is
difficult for large data set.
• There are many XML-oriented text editors that assist with the creation and
maintenance of XML documents.
• XML Spy
• EditiX
• Microsoft XML Notepad
• Visual XML
• XML Viewer
• Xeena
• XML Styler, Morphon, XML Writer…
Features of XML
• Structured Language : defines the structure of document using tags ,
user define language , it allows you to create your own tags.
• Platform independent: enables you to execute the XML applications
on any operating system.
• Excellent for long-term data storage and data reusability
• Language independent: implies that you ca use XML with any other
technology or language , such as java, .net and php
Comparing XML With HTML
Advantages of XML
• Stores data in the form of plain text.
• Provides the basic syntax that can be used to share information
among different applications.
• Allows you not to restrict to a limited set of tags defined by
proprietary vendors.
• It allows business documents to be viewed in a browser. Xml can
used on both client and server
Structure of XML Document
• XML defines certain rules for its syntax that specify how to create an
XML document.
• The syntax used to create is called markup syntax.
• The structure of XML document consist:
 XML declaration
 XML elements
 XML attributes
 XML tree
 XML comments
XML Document Structure
<?xml version = “1.0” encoding = “UTF-8” standalone = “no”?>
<!– this is an employee element-

<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.

<Person gender = “male” color= “dark brown”>


</Person>
XML Tree
• An XML tree represents the elements of an XML document in a tree
structure.
• Root Element: contains all other elements and the content of an XML
document.
• Parent Element: contain other inherited elements. Can contain
multiple child elements.
• Child elements: referred to as element that contain in a parent
element.
• Siblings : refer to the element that are contained in single parent
• https://www.javatpoint.com/java-applet
• https://www.tutorialspoint.com/java/java_applet_basics.htm
• https://www.geeksforgeeks.org/java-applet-basics/
• https://www.includehelp.com/java-programs/applet.aspx

Potrebbero piacerti anche