Java Made Simple: Read HTML param Tag in Applet Example

HTML param: First let us write a simple Applet program and see the drawbacks of the coding. Following is the simple familiar applet program that prints "Hello World".

HTML file: File Name: Simple.html



Applet file: File Name: DemoApplet.java

import java.applet.Applet;
import java.awt.Graphics;
public class DemoApplet extends Applet
{
  String message = "Hello World";
  int firstNumber = 10;
  double secondNumber = 20.5;

  public void paint(Graphics g)
  {
    g.drawString(message, 50, 60);
    g.drawString("Hard coding, Product of two numbers: " + firstNumber * secondNumber, 50, 80);
  }
}

HTML param

The problem of above code is hard coding. In hard coding, the data is written directly in the code itself. Later it is not possible to change the data without recompilation or Programmer’s intervention. Because it is written in the code itself, the user cannot modify the code. See what happens if the message and two numbers change. In hard coding no flexibility exists. Let us modify the HTML and Applet code to pass HTML param of message and two numbers from HTML to Applet.

Here <param> tag is used. <param> tag is the child tag of <applet> tag. We write <param> tag within <applet> tag.

HTML file: File Name: Simple.html


  
  
  

Applet file: File Name: DemoApplet.java

import java.applet.Applet;
import java.awt.Graphics;
public class DemoApplet extends Applet
{
  String message;
  int firstNumber;
  double secondNumber;

  public void init()
  {			                 // reading from HTML param tag
    message = getParameter("displayMessage"); 
    String num1 = getParameter("number1");
    String num2 = getParameter("number2");
			                 // do parsing (if needed) after reading HTML param
    firstNumber = Integer.parseInt(num1);
    secondNumber = Double.parseDouble(num2);
  }

  public void paint(Graphics g)
  {                                      // print HTML param values
    g.drawString(message, 50, 60);
    g.drawString("Flexible code: Product of two numbers: " + firstNumber * secondNumber, 50, 80);
  }
}

HTML param

If the message or the two numbers change, they are modified in HTML file. HTML file does not require any compilation. Applet reads from HTML whatever exists by that time. If values are changed, no need to compile again.

The getParameter(String) method of Applet class takes a string parameter (of nothing but "number1" of name attribute of <param> tag) and returns a string (of 10 of value attribute of <param> tag, but in string form). For this reason, num1 and num2 are parsed. If required for display purpose only, parsing is not required.

Advantages of HTML param tag

  1. First and foremost advantage is applet becomes portable.
  2. The tag parameters give flexibility to Applet code.
  3. The parameter values are equivalent to command-line arguments of a Java application as they are used for input to a Java program.
  4. Defining parameter values, an applet can be customized.
  5. Passing different parameters, the same applet can be made to work in multiple situations (interesting point is without recompilation).
  6. The action of applet can be changed simply by declaring in HTML file.
  7. With HTML param values, the background and font of the browser can be changed to keep in consistent with the static code of HTML based Web site to fit in any design.

Leave a Comment

Your email address will not be published.