How to Put a Java Application Into a Web Page

By Joe Wandy

Web pages can include Java applications to provide additional interactivity.
i John Foxx/Stockbyte/Getty Images

Java is a popular programming language. Normally, it is used to create applications that run on desktops, servers or embedded systems. However, Java applications also can run on Web pages. This type of Java application is called an applet. A Web browser with the appropriate Java plug-in can display applets as part of a Web page. This allows Java applications that have been created as applets to be easily distributed over the Internet.

Use the Java Development Kit to create an applet class. An applet class is similar to a normal Java class, but it extends from a parent class called the "java.applet.Applet" class. This will provide the applet class with additional mechanisms to communicate with Web browsers and interact with Web users. Alternatively, extend the applet from a parent class called the "javax.swing.JApplet" class if you wish to construct the graphical user interface of the application using Swing components.

Deploy the applet class. Compile the applet class and package along with the necessary resources as a JAR file. The JAR file is a combination of the compiled class file and all its related resources into a stand-alone compressed file. Using a JAR file provides the benefits of security, efficiency, versioning and portability and is recommended by Oracle as a method of deploying Java applications on a Web page.

Create a JNLP file descriptor. JNLP stands for Java Network Launch Protocol and specifies how an applet can be run in the Web page by the Web browser.

Create the Web page to contain the applet. Add a script in the Web page to call the Deployment toolkit. This specifies which applet to run, the width and height of the applet on the Web page and the associated JNLP file. The following is an example of the script call in the Web page:

....

<script src="http://www.java.com/js/deployJava.js"></script>

<script>

    var attributes = { code:'components.DynamicTreeApplet',  width:300, height:300} ;

    var parameters = {jnlp_href: 'dynamictree-applet.jnlp'} ;

    deployJava.runApplet(attributes, parameters, '1.6');

</script>

....

Test the applet to ensure that it displays successfully in a Web page. Place the JAR, JNLP and Web page files in the same folder. Display the Web page using a Web browser that already has the appropriate Java plug-in installed. Programmers can also check the Java Console messages for debugging and error reporting purposes.

×