Deploying With the Applet Tag
If you are not sure whether your end users’ browsers will have the JavaScript interpreter enabled, you can deploy your Java applet by manually coding the HTML tag, instead of using the Deployment Toolkit functions. Depending on the browsers you need to support, you may need to deploy your Java applet using the or HTML tag. Check the W3C HTML Specification for details on the usage of these tags.
You can launch your applet using Java Network Launch Protocol (JNLP) or specify the launch attributes directly in the tag.
Preparing for Deployment
Follow the steps described in the Deploying An Applet topic to compile your source code, create and sign the JAR file, and create the JNLP file if necessary. The overall steps for deployment are still relevant. Only the contents of your HTML page containing the applet will change.
Manually Coding Applet Tag, Launching Using JNLP
The AppletPage_WithAppletTag.html page deploys the Dynamic Tree Demo applet with an tag that has been manually coded (meaning, the applet is not deployed using the Deployment Toolkit which automatically generates the required HTML). The applet is still launched using JNLP. The JNLP file is specified in the jnlp_href attribute.
Manually Coding Applet Tag, Launching Without JNLP
Using JNLP is the preferred way to deploy an applet, however, you can also deploy your applet without a JNLP file.
The AppletPage_WithAppletTagNoJNLP.html deploys the Dynamic Tree Demo applet as shown in the following code snippet.
- code is the name of the applet class.
- archive is the name of jar file containing the applet and its resources.
- width is the width of the applet.
- height is the height of the applet.
- permissions indicates if the applet runs in the security sandbox. Specify «sandbox» for the value to run in the sandbox. Specify «all-permissions» to run outside the sandbox. If the permissions parameter is not present, signed applets default to «all-permissions» and unsigned applets default to «sandbox».
Java Applet Basics
Note:
java.applet package package has been deprecated in Java 9 and later versions,as applets are no longer widely used on the web.
Let’s understand first how many Package does GUI support:
Throwback of making GUI application:
Java was launched on 23-Jan-1996(JDK 1.0) and at that time it only supported CUI(Character User Interface) application. But in 1996 VB(Visual Basic) of Microsoft was preferred for GUI programming. So the Java developers in hurry(i.e within 7 days) have given the support for GUI from Operating System(OS). Now, the components like button, etc. were platform-dependent(i.e in each platform there will be different size, shape button). But they did the intersection of such components from all platforms and gave a small library which contains these intersections and it is available in AWT(Abstract Window Toolkit) technology but it doesn’t have advanced features like dialogue box, etc.
Now to run Applet, java needs a browser and at that time only “Internet Explorer” was there of Microsoft but Microsoft believes in monopoly. So “SUN Micro-System”(the company which developed Java) contracted with other company known as “Netscape”(which developed Java Script) and now the “Netscape” company is also known as “Mozilla Firefox” which we all know is a browser. Now, these two companies have developed a technology called “SWING” and the benefit is that the SWING components are produced by Java itself. Therefore now it is platform-independent as well as some additional features have also been added which were not in AWT technology. So we can say that SWING is much more advanced as compared to AWT technology.
What is Applet?
An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works at client side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
Important points :
- All applets are sub-classes (either directly or indirectly) of java.applet.Applet class.
- Applets are not stand-alone programs. Instead, they run within either a web browser or an applet viewer. JDK provides a standard applet viewer tool called applet viewer.
- In general, execution of an applet does not begin at main() method.
- Output of an applet window is not performed by System.out.println(). Rather it is handled with various AWT methods, such as drawString().
Life cycle of an applet :
It is important to understand the order in which the various methods shown in the above image are called. When an applet begins, the following methods are called, in this sequence:
When an applet is terminated, the following sequence of method calls takes place:
Let’s look more closely at these methods.
1. init( ) : The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.
2. start( ) : The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Note that init( ) is called once i.e. when the first time an applet is loaded whereas start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).
3. paint( ) : The paint( ) method is called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored.
paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required.
Note: This is the only method among all the method mention above, which is parameterized. It’s prototype is
public void paint(Graphics g)
where g is an object reference of class Graphic.
Now the Question Arises:
Q. In the prototype of paint() method, we have created an object reference without creating its object. But how is it possible to create object reference without creating its object?
Ans. Whenever we pass object reference in arguments then the object will be provided by its caller itself. In this case the caller of paint() method is browser, so it will provide an object. The same thing happens when we create a very basic program in normal Java programs. For Example:
public static void main(String []args)<>
Here we have created an object reference without creating its object but it still runs because it’s caller,i.e JVM will provide it with an object.
4. stop( ) : The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.
5. destroy( ) : The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).
Creating Hello World applet :
Let’s begin with the HelloWorld applet :