How to Create Interactive Web Programs with Java | 4 | WebReference

How to Create Interactive Web Programs with Java | 4

How to Create Interactive Web Programs with Java

Excerpted from Sams Teach Yourself Programming with Java in 24 Hours by Rogers Cadenhead. ISBN 0672328445, Copyright © 2006. Used with the permission of Sams Publishing.

The difference between opening tags and closing tags is that closing tags begin with a slash (/) character and opening tags do not. In Line 8, <vendor> is the opening tag, </vendor> is the closing tag, and these tags surround the name of the vendor who created the application. Place your own name within these tags, taking care not to alter the <vendor> or </vendor> tags around it.

Some tags have an opening tag only, such as Line 11:


The <offline-allowed/> tag indicates that the application can be run even if the user is not connected to the Internet. If it was omitted from the JNLP file, the opposite would be true, and the user would be forced to go online before running this application.

In XML, all tags that do not have a closing tag end with /> instead of >.

Tags also can have attributes, which are another way to define information in an XML file. An attribute is a word inside a tag that is followed by an equals sign and some text within quotes.

For example, consider Line 9 of Listing 17.5:

<homepage href=””/>

This is the <homepage> tag, and it has one attribute, href. The text between the quote marks is used to set the value of this attribute to This defines the home page of the application—the web page that users should visit if they want to read more information about the program and how it works.

The LottoMadness JNLP file defines a simple Java Web Start application that does not make use of any Java features that require special permission.

In addition to the tags that have already been described, Listing 17.5 defines other information required by Java Web Start.

Line 1 designates that the file uses XML and the UTF-8 character set. This same line can be used on any of the JNLP files you create for applications.

Line 2 is a comment. Like other comments in Java, it’s placed in the file solely for the benefit of humans. Java Web Start will ignore it.

Lines 3–5 indicate where the JNLP file for this application can be found. The codebase attribute in Line 4 is the URL of the folder that contains the JNLP file. The href attribute in Line 5 is the name of the file. These lines indicate that the file is at the web address LottoMadness.jnlp.

Lines 6 and 12 use the <information> and </information> tags to surround information about the application. Tags can contain other tags in XML: This tag contains <title>, <vendor>, <homepage>, <icon/>, and <offline-allowed/> tags.

Line 10 indicates the location of the program’s icon, which uses the codebase attribute described previously. In this example, the file is at the web address http://

Lines 13 and 16 use the <resources> and </resources> tags to surround information about resources used by the application.

Line 14 indicates the version of the Java interpreter that should run the application: 1.5, which sets up the application to use Java 2 version 5 (which has the internal version number 1.5)

Line 15 indicates the application’s JAR file. This also uses codebase, so this file is in

Line 17 indicates which class file should be run to start the application: LottoMadness. Note that the .class file extension is not specified—only the name of the class itself.

Line 18 ends the definition of this JNLP file. All JNLP files must be contained within an opening <jnlp> tag and a closing </jnlp> tag.

After you have created this file, change Line 4 of Listing 17.5 so that it refers to the folder on a web server where your application’s JAR file, icon file, and JNLP file will be stored.

Upload all three of the files to this folder, then run your browser and load the JNLP file using its full web address. If your web server is configured to support Java Web Start, the application will be loaded and begin running, as in Figure 17.7.

If your server does not support Java Web Start, which is more likely than not because it is a relatively new technology, you may see the text of your JNLP file loaded in a page and the application will not open.

A web server must be configured to recognize that JNLP files are a new type of data that should be run as an application, not delivered to the user as text in a browser window. On an Apache web server, the server administrator can support JNLP by adding the following line to the server's mime-types (or .mime-types) file:

application/x-java-jnlp-file JNLP


Most of the hours in this book focus on applications, primarily because most Java programmers today don't do a lot of work designing applets for the World Wide Web.

Writing applets is a good way for beginners to develop their skills as Java programmers for the following reasons:

  • Applets are usually smaller in scope, making their creation a less daunting task.

  • You can find thousands of sample applets on the World Wide Web, including many with the source file available from which to learn.

  • You can make applets available to a global audience at low to no cost through the Web, exposing your work to more people who can offer comments and suggestions.


Q Is there a reason why the codebase attribute should be used in an applet tag?

A If all Java programs are grouped into their own subfolder, as indicated by codebase, this structure might improve the way a website is organized, but there's no other reason why using codebase is better than omitting it. The choice is a matter of personal preference.

Q What happens if the height and width specified for an applet don't leave enough room for the information that is displayed in the paint() method?

A The information will be drawn off-screen, beyond the edges of the applet window, and won't be visible at any point while the applet runs. Choosing the right dimensions for an applet is largely a matter of trial-and-error until you find the right size for both the height and width attributes of the applet tag. You can't resize an applet window from within a Java program, so the only way to control its size is by using the height and width attributes. Fortunately, you can change the web page's HTML without having to recompile the Java program.

Q What is the Peter Principle?

A The principle was an idea popularized in the book The Peter Principle: Why Things Always Go Wrong by Laurence Peter with Raymond Hull.

Peter, a Canadian-born author, psychologist and professor, put forth the following principle: In the workplace, every employee rises to his own level of incompetence.

If an employee does a good job, he is repeatedly promoted until he eventually reaches a level where he cannot do the work—and is no longer promoted.

I was promoted to write computer books in 1996 and have remained in the position for nine years.


The following questions test your knowledge of applets.


1. What type of argument is used with the paint() method?

  • a. A Graphics object

  • b. A Graphics2D object

  • c. None

2. Which method is handled right before an applet finishes running?

  • a. decline()

  • b. destroy()

  • c. defenestrate()

3. Why can't all variables needed in an applet be created inside the init() method?

  • a. The scope of the variables would be limited to the method only.

  • b. Federal legislation prohibits it.

  • c. They can be created there without any problems.


1. a. The Graphics object keeps track of the behavior and attributes needed to display things on-screen in the applet window. You might create a Graphics2 object inside the method, but it isn't sent as an argument.

2. b.

3. a. Variables that are used in more than one method of a class should be created right after the class statement but before any methods begin.


You can apply your applet programming knowledge with the following activities:

  • Write an applet in which the text that is displayed moves each time the applet window is repainted.

  • Install the Java Plug-in with your preferred browser, if you're on a Windows or Solaris system, and try the sample applets provided from Sun's Java Plug-in page.

To see a Java program that implements the first activity, visit the book's website at

Excerpted from Sams Teach Yourself Programming with Java in 24 Hours by Rogers Cadenhead. ISBN 0672328445, Copyright © 2006. Used with the permission of Sams Publishing.


Created: March 27, 2003
Revised: February 13, 2006