Table of Contents

Java <APPLET...></APPLET> Tag

1. Introduction

The HTML <applet> tag is used to embed the Java applet in an HTML document. The <applet> tag takes a number of attributes, with one of the most important being the code attribute. This code attribute is used to link a Java applet to the concerned HTML document. It specifies the file name of the Java applet. Applet program can be run using  appletviewer Class_name.html.

Syntax:

<APPLET
   attribute 1 = value 1
   attribute 2 = value 2
   .........
   .........  >
</APPLET>

Example:

<applet
  code="Hello.class"
  align="right"
  height="200"
  width="300">
</applet>

2. Passing parameters to Applets

We can supply user-defined parameters to an applet using <PARAM...> tags. Applet class provides a method named getParameter(). Parameters are passed on an applet when it is loaded. We can define the init() method in the applet to get hold of the parameters defined in the <PARAM> tags.

Example:

//ParamExample.html
<html>
<body>
<applet code="ParamExample.class" width="300" height="300">
<param name="msg" value="Welcome to applet">
</applet>
</body>
</html>

//ParamExample.java
import java.applet.Applet;  
import java.awt.Graphics;  

public class ParamExercise extends Applet{  

public void paint(Graphics g){  
String str=getParameter("msg");  
g.drawString(str,50, 50);  
}}

output:

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?