Create Dynamic Webpages with the Applet Tag

applet tagIn the beginning, the World Wide Web was a dull place. Every website was just a collection of documents to read. There were no cat videos to watch. There was no online radio to listen to. There was just text.

For a decade, this was how life online was. It wasn’t until the invention of Java that the World Wide Web and the Internet in general became what it is today. However, HTML, the language that creates websites, had no way to implement Java. It was designed to be static, just text on a page. That is when Sun Microsystems invented the applet tag. Browsers with the tag offered their users a world they never seen before: a dynamic world wide web that can do almost anything.

Learn Java programming at Udemy.com

The Java Programming Language

Developed by James Gosling at Sun Microsystems, Java was created to accomplish tasks that were either impossible to do or too complicated to do with C++. It was designed to work on any operating system running on any computer system without the need for recompiling, recoding, or any other modification. A simple compiled interpreted language, Java can create any application in the world, but it became famous for its applets.

We create Java programs as plain text files with the .class file extension. The following is an example of a Java program.

class HelloWorld {

Public static void main(String[] arguments) {

String geeting = “Hello worl!”;

System.out.println(greeting);

}

}

If you programmed in C++ or C#, the syntax should look familiar as Java uses the same syntax. Like C++ and C#, Java is also an object oriented language. You can create functions and objects in it to package common tasks under a single name. In fact, you need to use objects from the Swing family of classes to create Java applications for the Internet.

Java Applets

In my previous example, I created an application with a main function. Like with C++ and C#, Java applications all start from a main function which in turns starts and calls all the other functions and classes in the program. Java applets are the exception to this rule.

Java applets use three different starting functions. These functions handle everything from the initiation to the actual running of the program. These functions are init, start, and paint. Init runs when the applet first loads, and sets any variables the applet needs. Start runs the actually applet, while paint displays the outputs to the screen. Start and paint can run a number of time such as when the applet first runs, when the browser is refreshed, or manually from within the applet.

A Java applet looks something like this:

import java.awt*;

import java.swing.*;

class HelloWorld extends JApplet {

String greeting;

public void intit() {

greeting = “Hello world!”;

}

public void paint(Graphics screen) {

Graphics2D screen2D = (Graphics2D)screen);

Screen2D.drawString(greeting, 25, 50);

}

}

The applet tag lets you inset this applet onto your website page.

Create Java applets at Udemy

The Applet Tag

The applet tag is an extension to the HTML language we use to write out websites. It starts with the <applet> tag and ends with the </applet> tag. Inside this tag, you place all the instructions a browser needs to run your applet. When complete, the applet tag should look something like this:

<applet code=”HelloWorld.class” codebase=”javadir” height=”300” width=”400”>

This text appears if the browser does not support Java.

<param name=”greeting” value=”Hello world!” /> </applet>

In the applet tag, the code attribute identifies the class file containing your applet. The codebase attribute gives the directory location of your applet. Height and width dictate the size of your applet as rendered by the browser. That is all you need to create a proper applet tag for your applet. Everything else is optional. The text within the tag is for browsers that do not support Java and can be any valid HTML tag.

The param tag is where you set information your applet need to run properly. The name attribute sets the variable name for the setting while value sets its value. You can add as many param tags as your application needs.

You access the param information in your applet through the getParamter() function., The function takes a string containing the name attribute of your desired param and returns the value as a string. You can process the string further if you need it.

String display1 = getParameter(“greeting”);

The Object tag

The applet tag lets you insert any Java applet into your website pages, but it is no longer supported by the World Wide Web Consortium (W3C) or Oracle (the current owner of Java). You will also find that most browsers no longer support the tag either. This is because the applet tag is what we called a depreciated tag. This means that the tag was replaced with something better: the object tag.

The object tag offers the same features as the applet tag but allows you to run more stuff other than Java. With object, our applet above now looks like this:

<object data=”javadir/HelloWorld.class” classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"  height=”300” width=”400”>

This text appears if the browser does not support Java.

<param name=”code” value=”HelloWorld.class” />

<param name=”greeting” value=”Hello world!” />

</object>

The data attribute is required and sets the location of your Java applet. Everything else is optional, but not for all browsers. Some older browsers don’t understand the data attribute and need extra help to render your applet. The classid attribute and code param exist to handle these tasks.

The classid attribute is a browser specific setting. It sets the location of the Java plugin on the client’s computer, but it’s being phased out. The param tag with name set to code is required for browsers that need the classid attribute and sets the location of the class file. Both are depreciated in favor of the data attribute, and I expect we will see fewer browsers support them as we go.  Still, you might what to include them just in case. Fortunately, you can cascade object tags to hide these old elements from browsers that no longer need them.

Either way, I hope you now have some understanding of how to embed a Java applet to a webpage with the applet tag and its replacement the object tag. Both let you create dynamic webpages that you can’t do any other way. Just make sure your target browser can handle them before you add them to your pages.

Learn how to write webpages with HTML at Udemy.