Daniel Kerr Coms 463

advertisement
Daniel Kerr
Coms 463
An Applet is a small program embedded into another application. This program
was intended to bring multimedia to websites. An Applet is a combination of HTML tags
along with downloaded code. Typically, this code is java, but it can be written in other
languages. Java Applets can be very simple or extremely complex. Whether simple or
complex all Applets have a life cycle. Applets are initiated, started, and will eventually
stop and be destroyed.
As stated earlier Applets are often built using the Java programming language.
Java Applets are built into webpages to increase their functionality. HTML itself is
rather limited when it comes to GUI’s and multimedia. In order to work around some of
HTML’s shortcomings the <APPLET> tag was devised. This tag allows a webpage,
which is built with HTML, to run an applet on a client’s computer. However, this is not a
complete workaround. Applets do not have to be written in Java. There are many
examples of other languages being used to write applets. So why use java? It becomes a
question of availability. Since an applet is a program, it needs software to run, a run-time
environment. This software is called a plug-in and is downloaded for use within a
browser to provide that environment. Without the proper plug-in, a browser would see
the <APPLET> tag and simply skip past it. Java simply has the most readily available
internet browser plug-ins. The Java Virtual Machine (JVM) is the plug-in the browser
uses to run compiled java code.
Before we run right into the code aspect of the Applet, we need to understand how
is placed on a webpage. The <APPLET></APPLET> tag set is used to tell the browser
Daniel Kerr
Coms 463
to set aside space for an applet. Attributes such as WIDTH and HEIGHT are used to
create the exact size of the display the applet will have access to. The CODE attribute is
used to tell the webpage where the applet code is, and the name of that code.
<APPLET CODE=”HelloWorld.class” WIDTH=150 HEIGHT=25></APPLET>
In this example, the browser would set aside a 150x25 pixel display area and would run
the HelloWorld.class. The output of the HelloWorld code would be displayed in the
applet. Another tag associated with the applet is <PARAM NAME=parameter1
VALUE=avalue>. This allows the user to set the values of those parameters.
Browsers have two main ways of dealing with not understanding applets. If the
browser itself simply does not understand the applet tag, it will display alternate HTML
code. This code is placed right before the closing applet tag and should simply inform a
user that an applet should appear there.
<APPLET CODE=”HelloWorld.class” WIDTH=150 HEIGHT=25>
Your browser is ignoring the applet tag
</APPLET>
The second approach is if the browser does understand the applet tag, but does not
have the correct plug-in to run the applet. In this case, a special attribute can be added to
the applet tag called alt. The browser will display the information in the alt attribute if
the applet cannot be run.
<APPLET CODE=”HelloWorld.class” WIDTH=150 HEIGHT=25
alt=”Your browser cannot display the applet”>
</APPLET>
These cases are mutually exclusive. One browser can not display both of these
alternates. Either the browser understands the applet tag and displays the alt information,
Daniel Kerr
Coms 463
completely skipping the alternate HTML code, or it cannot understand the applet tag and
only runs the alternate HTML code.
The final issue involving applets and their HTML component is the location of
the class file. In the example given above, the browser assumes the class is in the same
directory as the HTML file. However, the class file does not have to be in the same
directory. In fact, the class file does not even have to be on the same server. Using
another attribute of the <APPLET> tag called codebase we can tell the browser exactly
where to look for the class file. For example if we were to add the line codebase=”docs/”
to the <APPLET> tag above, the browser would look for a folder called docs in the same
directory as the HTML file then search for the class file. Using codebase we can even
tell the browser to look at a different URL for the class file.
Now that I have gone over the HTML side of applets, I will explain about the
code side. In Java the class used for an applet is actually a sub class of the applet class.
The code will import java.applet.Applet and when declaring the class it will extend the
applet class. See example A at the end of the paper for sample code. All applets have a
“life cycle”. What happens when the browser runs the class is an instance of that class is
created and initialized. The applet then starts running. There are several ways to stop the
applet. Upon exiting the browser window, the applet is destroyed. There are several
methods associated with this lifecycle that the applet subclass inherits from the applet
class. These methods include init(), start(), paint(), stop(), and destroy(). Every applet
will need to override either init, start, or paint. Init is very similar to a class’s constructor,
Daniel Kerr
Coms 463
it is run each time the applet loaded. Start and Paint are very similar to class’s main
method. This is where the program starts executing code after the applet has been
initialized. The stop and destroy methods are centered around exiting the applet and
cleaning up. Stop can be used to simply pause the applet’s execution, as where destroy is
used as a final cleanup before the applet is unloaded. There are multiple ways to invoke
the stop method. One such way is to minimize the browser. This should not destroy the
applet, but rather pause it, so the applet is not taking up system resources for a page that
is not being viewed. For many applets, the stop method is enough to clean up after the
applet. In some cases the destroy method is needed however those cases are beyond the
scope of this paper.
From here, we get into some more complex coding options that are available.
This paper was meant to give the reader the basic tools needed to create only the simplest
applets, however I will touch on a few advanced features. Applets have a couple of basic
display methods, Paint and Update. The Paint method takes an argument graphics object.
This object is the display area the browser has reserved for the applet. The graphics
object has several drawing methods. The update method is to be used along with paint to
improve drawing performance. The applet class inherits from the AWT Container class.
This class gives applets the ability to place and use User Interface components. Such
components include buttons, checkboxes, labels, scrollbars and various others. Once
placed on the applet the user would then be able to use these components much like a VB
form.
Daniel Kerr
Coms 463
Obviously, I have only scratched the surface in terms of what applets can be
coded to do. Applets do have limits, mostly due to security reasons. Applets cannot
ordinarily read or write files on the client that it is running on. It cannot make network
connections except to the host it came from. It cannot start and program on the client that
is executing it. It cannot read certain system properties. The browser the applet is
running on usually enforces these and other restrictions. It is predicted that in the future
the <APPLET> tag will be replaced with something more generic like an <OBJECT>
tag. Such a change would allow websites more flexibility in terms of what code it will
attempt to run. But for now, we have HTML and Java Applets.
Example A:
This code was taken from Java’s website.
import java.applet.Applet;
import java.awt.Graphics;
public class Simple extends Applet {
StringBuffer buffer;
public void init() {
buffer = new StringBuffer();
addItem("initializing... ");
}
public void start() {
addItem("starting... ");
}
public void stop() {
addItem("stopping... ");
}
Daniel Kerr
Coms 463
public void destroy() {
addItem("preparing for unloading...");
}
void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint();
}
public void paint(Graphics g) {
//Draw a Rectangle around the applet's display area.
g.drawRect(0, 0, size().width - 1, size().height - 1);
//Draw the current string inside the rectangle.
g.drawString(buffer.toString(), 5, 15);
}
}
Questions
Short Answer Q’s
1)
Why were applets introduced to HTML?
2)
Why are most applets coded in Java?
3)
Please list two security restrictions placed on applets?
4)
Please describe one of the two cases in which a browser would be unable to run
an applet?
5)
Please list the milestones in an applets life
Multiple Choice
1)
Most applets are written using
a) Java
b) XML
c) Unix
d) SQL
2)
The <alt> tag is use for
a) Browsers without the proper plug-in
b) Browsers that cannot read the <APPLET> tag
Daniel Kerr
Coms 463
3)
4)
5)
c) Browsers that can read the <APPLET> tag and have the proper plug-in
d) Is not used in applets
What is the correct order of the applet lifecycle
a) init(), close(), start(), destroy()
b) destroy(), close(), start(), init()
c) close(), init(), start(), destroy()
d) init(), start(), close(), destroy()
Applets can do all of the following except
a) Read or Write files on the client
b) display graphics
c) render animation
d) make use of objects like scrollbars, and textboxes
What does the init() method do?
a) It creates a display area for the applet
b) It stops all other applets from running
c) It initializes the applet code
d) It gives up all system resources used by the applet
Fill in the blank
1)
An applet is made of 2 parts. The
and
.
2)
Most applets are programmed in
because of the availability of browser plugins
3)
The
attribute or
HTML is used incase a browser cannot display
the applet
4)
Browsers need the appropriate
to run an applet
5)
Applets are designed to bring
to webpages.
Answers:
Short-answer Q’s
1) -to increase functionality. Can also say to bring multimedia or GUI’s to webpages
2) -Browser plug-ins for java are more available.
3) -any 2 of the following. Cannot read/write files, make network connections other then
to the host, cannot access system properties, cannot run other programs on the client
4) -The browser is unable to read the <Applet> tag(the html code) or The browser is
unable to run the code downloaded(it is missing the correct plug-in)
5) -Initialization (init()), Start running (start()), Stop execution (close()), Clean up and
unloading (Destroy())
Multiple Choice:
1) a) Java
2) a) Browsers without the proper plug-in
3) d) init(), start(), close(), destroy()
4) a) Read or Write files on the client
5) c) It initializes the applet code
Daniel Kerr
Coms 463
Fill in the blank:
1) HTML and CODE
2) Java
3) Alt or Alternate
4) plug-in
5) multimedia or GUI’s (either is correct)
Download