Please support my sponors and make this site possible!!!
Please support our sponsors!

 

Home > Core Java FAQ > Applets FAQ
Applets
Applets Vs Applications(04)  * Installing Applets(10) * User Interface(12) * Program Structure(10)  * Applet Communication(05) * Exceptions(06) * Miscellaneous(08) 
 
Q . What is an applet?

Ans : 

An applet is a Java™-compatible program that you can embed in a web page.

     An applet itself is fairly simple; it is usually composed of several pieces:

  • code: the Java class files that represent the executable code of the applet
  • other resources: data needed by the applet, including images and sounds

These pieces, however, only begin to tell the story. The real action happens when you bring in the applet's supporting cast:

  • the applet context
  • the APPLET tag
  • http servers and the Internet

     To execute applets, you need an applet context—a larger program, typically a Java-enabled web browser, that automatically finds and loads the applet code across the network and then runs the applet code locally. The applet context must know how to interpret an APPLET tag—an html (HyperText Markup Language) tag that points to the applet's executable code and provides information on how to run the applet. In addition, the applet context defines a security policy to prevent arbitrary applets from harming the host system.
     Finally, to deliver applets across the Internet, you need a standard http server that can deliver the applet code and resources across the network when requested.
     An applet is much more than just a fancy way to spice up a web page. It is real application code, running on the user's machine, complete with a graphical user interface. Correspondingly, an applet can:

  • draw interactively on screen, rather than merely presenting static images
  • respond directly to the user's keyboard and mouse events
  • perform calculations on the user's machine

In other words, an applet transforms a rectangular area of a web page into a fully interactive computational engine powered by the Java Virtual Machine.

Q . How do applets differ from applications?

Ans : 

Applications are stand-alone, full-featured programs, whereas applets are embeddable, almost full-featured programs.

     Java applets and Java applications have much in common, but there are clear differences as well. Table 4.1 summarizes some of the key ones.

 

Table 4.1: Java Applications versus Applets
Java application Java applet
must be installed on local machine needs no explicit installation on local machine
must be run explicitly within a Java-compatible virtual machine loads and runs itself automatically in a Java-enabled browser
can run with or without a graphical user interface must run within a graphical user interface (using the Abstract Window Toolkit)
starts execution with its main method starts execution with its init method
once started, manages its own flow of execution has its flow of execution determined partly by its browser context 
has no inherent security restrictions (apart from safety features in the Java language itself) has significant security controls to prevent malicious or poorly written applets from harming the user's system

     Note: Differences arising from applet security are extensive and are detailed separately in JavaSoft's Security FAQ web page.

Q . Can I write Java code that works both as an applet and as a stand-alone application?

Ans :  

Yes, but with limitations.

     Applets and applications share the standard Java class libraries, including the user-interface tools in the Abstract Window Toolkit (AWT). These classes let you present a user interface, handle events, manage input and output (subject to security restrictions), and generally define objects and their interactions.
     To work as a combined applet/application, your Applet subclass must include a main method; main is the required starting point for all applications:

     public static void main(String[] args) { ... }

     Your main method must create an applet instance and place it within a frame (the AWT class for a top-level window with borders). This method also needs to call the applet's init, start, stop, and destroy methods at appropriate times to simulate how a browser would have invoked those methods; a code skeleton (written for JDK 1.1) illustrating these method calls follows:

     /* using JDK 1.1: */
     import java.awt.*;
     import java.awt.event.*;
     import java.applet.Applet;
     public class AppletAndApplicationExample extends Applet {

         public void init() { /* ... (Q4.16, Q4.17) ... */ }

         public void start() { /* ... (Q4.16, Q4.18) ... */ }

         public void stop() { /* ... (Q4.16, Q4.18) ... */ }

         public void destroy() { /* ... (Q4.16) ... */  }

         public static void main(String[] args) {
               /* Set up the frame that holds the applet. */
             Frame appletFrame = new Frame("My Applet");
             Applet theApplet = new AppletAndApplicationExample();
             appletFrame.pack();  // create peer frame (hack)
             appletFrame.add(theApplet, "Center");  // applet fills
                                                    // frame
             appletFrame.setLocation(40, 40);
             appletFrame.addWindowListener(new AppQuitter());

             /* Now start invoking applet methods. */
             theApplet.init();
             appletFrame.pack();  // resize frame to fit initialized
                                  // applet
             appletFrame.show();  // bring frame and initialized
                                  // applet on screen
             theApplet.start();
             theApplet.stop();
             theApplet.destroy();
         }
   
         /** An inner class for quitting the application. */
         static class AppQuitter extends WindowAdapter {
               public void windowClosing(WindowEvent e) {
                     e.getWindow().dispose();
                    System.exit(0);
               }
          }
     }

     Combined applets/applications are interesting and useful (in testing your code, for example), but there are considerable limitations on what functionality is common to both sides. Because of applet security restrictions, applications can do much more than applets. On the other hand, applets running in Java-enabled browsers have some capabilities that are not available to applications in the JDK (1.0.2 or 1.1). Table 4.2 notes some of these differences.

 

Table 4.2: Some Differing Application and Applet Capabilities
Applications only reading from and writing to the local file system
making socket connections to arbitrary hosts on the net
loading native code (e.g., functions written in C)
Applets only loading and playing audio 
communicating with applets on a web page

For further details on applet security restrictions, consult the JavaSoft Security FAQ at http://java.sun.com/sfaq/.

Q . What is the difference between an application, an applet, and a servlet?

Ans :  

An applicationis a standalone program.
An applet is a downloadable program that runs in a web browser. Typically an applet has restricted access to the client system for reasons of security. Other than that it is virtually no different from
a regular Java program.
A servlet is a Java program whose input comes from a server and whose output goes to a server. Other than that it is virtually no different from a regular Java program. Think of a servlet as an application, but
one that (like an applet) requires a context in which to run, namely web server software. Servlets are used like CGI scripts, but take much less processor resources, and they allow the server end to be written in Java as well as the client. There is a page with much servlet information at: http://www.frontiernet.net/~imaging/servlets_intro.html

There is a servlet tutorial at
http://java.sun.com/docs/books/tutorial/servlets/index.html
and another servlet tutorial at
http://www.novocode.com/doc/servlet-essentials/
There is a servlet FAQ at http://www.saint-elie.com

The web server starts up a servlet when the URL is referenced, and now your applets have something that they can talk to (via sockets) on the server that can write files, open connections to other servers, or
whatever. There is also a software technology from IBM called an "Aglet". An aglet is a mobile agent that can go from machine to machine, performing tasks, serializing data collected, and "shipping itself"
(code and data) to the next machine. It's too early to say if aglets are a flash in the pan or a dawning technology. Read about aglets at http://www.trl.ibm.co.jp/aglets/
Finally, there is the ticklet (Tcl/Tk) plugin for your browser (Netscape or Explorer) available at http://sunscript.sun.com/plugin/
Don't confuse Sun's JWS "Java Web Server" with JWS "Java Workshop". Java Web Server supports servlets, as does the lightweight and free server at Acme.com:

 

Copyright © 2000 javafaq.com. All rights reserved