SCJP Java Programming Exam Cram Notes : java Applets And Html.

Between </applet> <APPLET> and </APPLET>, PARAM tags can be specified. These are used to pass parameters from HTML page to the applet.

<PARAM NAME = "name" VALUE = "value">

Applets call getParameter(name) to get the parameter. The name is not case sensitive here.

The value returned by getParameter is case sensitive, it is returned as defined in the HTML page.

If not defined, getParameter returns null.

Text specified between <APPLET> and </APPLET> is displayed by completely applet ignorant browsers, who cannot understand even the <APPLET> tag.

If the applet class has only non-default constructors, applet viewer throws runtime errors while loading the applet since the default constructor is not provided by the JVM. But IE doesn't have this problem. But with applets always do the initialization in the init method. That's the normal practice.

Methods involved in applet's lifecycle.

Method Description
void init() This method is called only once by the applet context to inform the applet that it has been loaded into the system. Always followed by calls to start() and paint() methods. Same purpose as a constructor. Use this method to perform any initialization.
void start() Applet context calls this method for the first time after calling init(), and thereafter every time the applet page is made visible.
void stop() Applet context calls this method when it wants the applet to stop the execution. This method is called when the applet page is no longer visible.
void destroy() This method is called to inform the applet that it should relinquish any system resources that it had allocated. Stop() method is called prior to this method.
void paint(Graphics g) Applets normally put all the rendering operations in this method.

Limitations for Applets:

  • Reading, writing or deleting files on local host is not allowed.
  • Running other applications from within the applet is prohibited.
  • Calling System.exit() to terminate the applet is not allowed.
  • Accessing user, file and system information, other than locale-specific information like Java version, OS name and version, text-encoding standard, file-path and line separators, is prohibited.
  • Connecting to hosts other than the one from which the applet was loaded is not permitted.
  • Top-level windows that an applet creates have a warning message for applets loaded over the net.

Some other methods of Applet class

Method Description
URL getDocumentBase() Returns the document URL, i.e. the URL of the HTML file in which the applet is embedded.
URL getCodeBase() Returns the base URL, i.e. the URL of the applet class file that contains the applet.
void showStatus(String msg) Applet can request the applet context to display messages in its "status window".

Previous    Contents    Next