Sie sind auf Seite 1von 16

SEMINAR REPORT

ON

APPLICATION OF JAVA APPLETS IN WEB DESIGNS

PRESENTED BY

OGBODO ANTHONY EMEKA


REG. NO: FPI/ND/COM/08/500

SUBMITTED TO

THE DEPARTMENT OF COMPUTER SCIENCE


SCHOOL OF TECHNOLOGY
FEDERAL PLYTECHNIC IDAH,
KOGI STATE.

IN PATIAL FULFILMENT FOR THE AWARD OF NATIONAL


DIPLOMA (ND) IN COMPUTER SCIENCE.

SUPERVISOR: MR TUNDE AKILAPA

MARCH, 2010.

1
Ogbodo Anthony Emeka +2348068573605
ABSTRACT

Web design has moved to the next level with the introduction of java technology in the

computing and programming industry. This introduction has made a tremendous impact in

the capabilities and functionalities of web pages. Now using not only HTML codes but java

scripts and java applets, more interactivities and functionalities can be added to web pages.

This seminar paper looks at what a java applet is, how to write applet codes and how to

deploy them on web pages.

2
Ogbodo Anthony Emeka +2348068573605
INTRODUCTION
This seminar paper discusses the basics of applets, how to deploy them. These applets

interact richly with their environment.

An applet is a special type of java program that a browser enabled with java technology can

download from the internet and run. An applet must be a sub class of the java.applet.Applet

class. The applet class provides the standard interface between the applet and the browser

environment. Swing provides a special sub class of the Applet class called

javax.Swing.JApplet. The JApplet class should be used for all applets that use the swing

components to construct their graphical user interface. The browser’s java plug-in software

manages the life cycle of an applet.

Most of today’s browsers can run java applets. The ones that cannot run are not of much

importance since very few users have such outdated browsers. To be more precise, applets

can be embedded in pages viewed by netcape2 and newer versions, internet explore 3 and

newer version, and others. However, some people have turned off the ability to run applets in

their web browsers. These in most cases are companies with more or less paranoid ideas of

potential hacking. No matter what their motivation is, the fact is that there are minor people

who will not view your applet. This should be taken into consideration before deciding to add

applets to your web pages.

3
Ogbodo Anthony Emeka +2348068573605
DEFINING A SUB CLASS OF AN APPLET

Every applet must define a sub class of Applet or JApplet class. Every java applet is a

graphical user interface on which you can place graphical user interface (GUI) components.

They inherit the significant functionality from the applet or JApplet class including the

capability to communicate with the browsers and present a graphical user interface (GUI) to

the user. Below is an example of a simple java program.

A SIMPLE JAVA APPLET: DRAWING A STRING.

1 Import java.awt.Graphics; // program imports class graphics.

2 Import javax.Swing.JApplet; // imports class JApplet.

4 public class DisplayText extends JApplet {

5 // draws text on the background.

6 public void paint (Graphics g) {

7 // call super class version of the method paint.

8 super.paint (g);

9 // draw a string at x-coordinate 25 and y- coordinate 25.

10 g.drawString (“This is my first java applet”, 25, 25);

11 } // end method paint.

12} // end class welcome.

Line 1 imports graphics to enable the applet to draw graphics such as lines, rectangles, ovals

and strings of characters. The class JApplet (imported at line 2) from package javax.Swing is

used to create applet. As applications, every java applet contains at least one public class

4
Ogbodo Anthony Emeka +2348068573605
declaration. An applet container can create only objects of classes that are public and extends

JApplet (or the applet class from earlier versions of java). For this reason, class DisplayText

(line 4-12), extends JApplet.

An applet container expects every java applet to have method named init, start, paint, stop

and destroy each of which is declared in the class JApplet. These methods can be overridden

(refined) to perform tasks that are specific to your applet. When an applet container loads

class DisplayText, the container creates an object of type DisplayText then calls three of the

applet methods. In sequence, these three methods are init, start and paint. If you do not

declare the methods in your applet, the applet container calls the inherited versions. The

super class methods init and start have empty bodies so, they do not perform any task. It is

necessary for the applet container to inherit these methods init, start and paint even when

some do not make any use of them so that it will have a consistent start up sequence as other

applications start execution with the main method. This inheritance ensures that the applet

container executes each applet uniformly. Also, inheriting the default implementation of

these methods allows the programmer to concentrate on drafting only the methods required

for a particular applet. The method paint() was overridden in order to draw the string (line 6-

11) by placing statements in the body of the paint that draws a message on the screen. Line 8

calls the super class method paint that was inherited from JApplet. This statement should be

the first in any applet paint method, omitting it can cause subtle errors in applets that

combine drawing and GUI components. Line 10 uses graphics to draw the string “This is my

first java applet” at x-coordinate of 25 and y-coordinate of 25 on the drawing area.

5
Ogbodo Anthony Emeka +2348068573605
APPLET LIFE-CYCLE METHODS

An applet class provides a frame work for execution, defining methods that the system call

when milestone occur. Milestones are major events in an applet life cycle. Most applets

override some or all of these methods to respond appropriately to milestones.

These methods are explained below. Other than method paint (), these methods have empty

bodies by default. If you want to declare any of these methods and have the applet container

call them, you must use the method headers shown below. If you modify the header (e.g. by

changing the names or by providing parameters), the applet container will not call your

methods instead it will call the super class method inherited from JApplet.

public void init ()

This method is called once by the applet container when an applet is loaded for execution.

This method initializes an applet. The action performed her are purely initialization of field,

creating GUI components, loading sound to play, loading images to display and creating

threads. Keep your initialization method so short that your applet may load quickly.

public void start ()

Every applet that perform tasks after initialization (except in direct response to user actions.)

must override the start method. It is good to practice to return quickly from the start method.

If you need to perform computionally intensive operations, it might be better to start a new

thread for this purpose. This method is called by the applet container after the initialization

method. Action performed might include starting an animation or starting other threads of

execution.

6
Ogbodo Anthony Emeka +2348068573605
public void paint (Graphics g)

This method is called after init () and start () methods. The method is also called when an

applet needs to repainted. Example is if a user minimizes or cover an open page containing

applet and uncovers it, the paint method is called. Action performed involves drawing with

the graphic object g that is passed to the paint method by the applet container.

public void stop ()

Most applets that override the start method should also override the stop method. The stop

method should stop the applet’s execution to save memory and resources when the user is not

viewing it. Example, an applet that draws animation should stop drawing it when the user is

not viewing it. Typically, action performed here would stop the execution and threads.

public void destroy ()

This method is called by the applet container when the applet is removed from the memory.

There is no need to override the destroy method because the stop method which is called

before destroy will perform tasks necessary to shut down the applet’s execution. This occurs

when the user exits the session by closing all the browser windows. The method performs

any task that is required to release additional resources allocated to the applet.

7
Ogbodo Anthony Emeka +2348068573605
APPLET’S EXECUTION ENVIRONMENT.
An applet runs in the context of browser. Java plug-in software in the browser controls the
loading and execution of the applet. The brower also has the JavaScript interpreter which
runs the JavaScript code on a web page.
Java plug-in
The java plug-in created a working thread for every applet. It loads the applet in an instance
of a java runtime environment (JRE) software. Normally all applets run in the same instance
of JRE. The java pug-in software starts a new instance of the JRE in the following cases.
1. When an applet requests to be executed in a specific version of the JRE.
2. When an applet specifies its own JRE start up parameters. For example, the heap size.
A new applet uses an existing JRE when its requirements are a subset of the existing
JRE otherwise, a new instance is started.
Java plug-in and java script interpreter interaction.
Applets can invoke JavaScript function present in the web page. JavaScript functions are also
allowed to invoke methods of an applet embedded on the same web page. The java plug-in
software and the java interpreter calls from JavaScript code to java code and from calls from
java code to JavaScript code. The java plug-in software is multithreaded while the java
interpreter runs on a single thread. Hence, to avoid thread related issues especially when
multi applets are running simultaneously. Keep the calls between java code and JavaScript
code short to avoid round trip.

THE THREAD CLASS


A thread is an independent sequential flow of control within a process. The threads run
within program. Single application or applets can have many threads doing different things
independently. The thread class is defined in Java.lang package. To use threads in your
program, you need to define your own local extension of the thread class and overrides its
run () method. When an object is controlled by a thread, It’s run () method should be invoked
by that thread. This will happen automatically when the threads start method is invoked if the

8
Ogbodo Anthony Emeka +2348068573605
object class implements the runnable interface. Accordingly, applets that run animation by
means of thread normally implement runnable.

A SIMPLE APPLET THAT WILL DISPLAY A DIGITAL


CLOCK ON A WEB PAGE.
1. import java.awt.*;
2. import java.text.DateFormat;
3. import java.util.Date;
4. import javax.swing
5.
6. public class clock extends JApplet implement Runnable {
7. MainPanel panel;
8. Thread thread;
9.
10. public void init() {
11. setSize( 400, 800);
12. panel = new MainPanel();
13. getContentPane().add(panel);
14. }
15.
16. public void run() {
17. while (thread != null ) {
18. try {
19. Thread.sleep(10);
20. } catch (InterruptedException e) {
21. }
22. panel.repaint();
23. }
24. }
25.
26. public void start() {
9
Ogbodo Anthony Emeka +2348068573605
27. if ( thread == null) {
28. Thread= new thread(this);
29. Thread.start();
30. }
31. }
32.
33. public void stop() {
34. Thread = null;
35. }
36. }
37.
38.
39. class MainPanel extends Jpanel {
40. DateFormat dateformat = Date.getTimeInstance(DateFormat.DEFAULT);
41.
42. public void paintComponent(Graphics g);
43. super.paintComponent(g);
44. g.setFont(new Font(“Monospaced”, Font.BOLD, 50));
45. g.drawString(Date.toString, 50, 50));
46. }
47. }

The applet starts execution in line 10 with its init() method. It sets the size of the applet
container, instantiates the main panel, and adds it to the applet’s content pane. Then the
applet’s start() method invoked(automatically by the browser). Since the thread is initially
null, this creates a new thread and starts it in line 29. Since the applet class implement
runnable, the thread’s start() method invokes the applet’s run() method which invokes the
panel’s repaint() method(at line 22) every 10 milliseconds.
The JPanel class’s repaint() method calls its paint() method which calls its paintComponent()
method. This is overridden in MainPanel class at line 42. It calls the JPanel class’s
paintComponent() method at line 43, sets a big and bold font in line 44, and then finally
draws the current time at line 45.
10
Ogbodo Anthony Emeka +2348068573605
TESTING AN APPLET
For testing purpose, an applet viewer is most convenient. The java software development kit
(java SDK) includes an applet viewer named “applet viewer.exe”. it is located in the bin
directory of the SDK. To run the program example above, first, compile the program to get
the clock.class bytecode file, then enter this at the command prompt
appletviewer clock.html
The applet class should launch and display the applet. You can also use a web browser to test
your applet.

DEPLOYMENT OF AN APPLET
After the compilation of your applet which can be done in two ways:
1. By using the command prompt: Type your applet code in word editor like note pad
and save it with .java. Enter the command prompt and type “javac filename.java”. the
compiler will generate a class file (i.e. filename.class).
2. By using an integrated development environment (IDE): After typing code in a text
editor of the IDE, use the compile feature of the IDE to compile the source code to a
class format.
Design your web page as you like and put the code as below;

A WEB PAGE WITH JAVA APPLET DEPLOYED.


<html>
<title> clock </title>
<body>
Current Time! <br>
<hr>
<applet code = “clock.class” width = 300 height = 60 >
</applet>
<hr>
</body>
</html>

11
Ogbodo Anthony Emeka +2348068573605
The essential codes are lines 6-7. This is an applet tag with three attributes: code, width, and
height. The values for these three attributes are “clock.class”, 300 and 60, respectively each
is assigned on line 6. This tells the HTML environment (browser or applet viewer) to run the
clock.class applet in a from 300 pixels wide and 60 pixel high. Note that the program is
enclosed between the <html> tag(line) and </html> tag (line10). The web page has two main
part, its title and its body (line 2 and 3-9 respectively)

WHAT APPLET CAN AND CANNOT DO


Applets are loaded on a client when the user visits a page containing an applet. The security
model behind applet has been designed with the goal of protecting the user from malicious
applets.
Applets that are not signed using a security certificate are considered to be untrusted and
referred to as unsigned applets. When running on a client, unsigned applets operates within a
security sandbox that allows only a set of safe operations.
Applet can be signed using a security certificate to indicate that it came from trusted source.
Signed applets operate outside the security sandbox and have extensive capabilities to access
the client. A signed applet will run outside the security sandbox only if the user accepts its
security certificate. Below are the security restrictions and capabilities of applets.
1. Unsigned Applets: Unsigned applet can perform the following operations;
• They can make network connections to the host they came from.
• They can easily display HTML documents using the showDocument() method
of the java.applet.AppletContext class.
• They can invoke public methods of other applets on the same page.
• Applets that are loaded from the local file system (from a directory in the
user’s CLASSPATH) have none of the restrictions that applets loaded over
the network do.
• They can read secure system properties.
• When launched by using java network Launch protocol (JNLP), unsigned
applets can also perform the following:
o They can open, read, and save files on the client.

12
Ogbodo Anthony Emeka +2348068573605
o They can access the system-wide clipboard.
o They can access printing functions.
o They can store data in the client; decide how applets should be
downloaded and cached, and much more.

Unsigned applet cannot perform the following operations


 They cannot access client resources such as the local file system, executable file,
system clipboard and printers.
 They cannot connect to or retrieve resources from any third party server (any server
other than that of origin).
 They cannot load native libraries.
 They cannot change the security manager.
 They cannot create a class loader.
 They cannot read certain system properties.

SIGNED APPLETS.
They do not have the security restrictions imposed on unsigned ones and can run outside the
security sandbox.
Note: when a signed applet is accessed from JavaScript code in an HTML page, the applet is
executed within the sandbox. This implies that signed applet essentially behaves like
unsigned applet.

DISADVANTAGES OF APPLETS.
Before allowing an applet to run, make sure you know and trust the source as it may harm
your system because some programmers use it as a harking tool, a means to introduce
viruses, and can act as a spyware or adware to your system. It needs a java plug-in on your
browser before it can be able to run.
ADVANTAGES OF APPLETS.
1. Java applets add interactivity to web pages.
2. Allow application to run on web pages like playing games while browsing, chatting
on web pages as applets have network connectivity ability.

13
Ogbodo Anthony Emeka +2348068573605
CONCLUSION

Having strategically discussed java applets, its development, deployment, advantages and

disadvantages, it can be seen that writing java applet codes are very simple to do as well as

can add functionality and interactivity to your web pages.

14
Ogbodo Anthony Emeka +2348068573605
RECOMMENDATIONS

When designing a web page that you need that some logical applications run on, using java

applet is more convenient. Finally, when designing a java applet, bear in mind that there are

some fractions of people that will not view your applet, also before allowing an applet to run

on your browser, make sure that you know and trust its source or that it is signed.

15
Ogbodo Anthony Emeka +2348068573605
REFERENCES

Deitel (2007): Java How to Program Seventh Edition, Pearson Education, Inc.

USA.

John .R. Hubbard, Ph.D (2004): Programming with Java Second Edition. The

McGraw-Hill Companies, Inc. USA.

Lesson: Applet (The Java Tutorials > Deployment).

http://java.sun.com/doc/books/tutorial/deployment/applet/,

(Retrieved 15/02/2010)

16
Ogbodo Anthony Emeka +2348068573605

Das könnte Ihnen auch gefallen