Table of contents
1.
Introduction
2.
JSP Applet Plugin
3.
Attributes
4.
Displaying applet in JSP 
4.1.
Steps for displaying applet in JSP
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Displaying Applet in JSP

Author Parth Jain
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An Applet is a unique program used to generate dynamic webpage content. It is embedded inside the webpage and can run on a browser to perform client-side rendering. Sun Microsystems established applets in the year 1995.
As an Applet runs on the client-side, they have less response time and are more secure. Also, As applets run on a browser, they are platform-independent.

JSP Applet Plugin

To plugin an Applet, we need to use a JSP action tag called jsp:plugin. The JSP plugin embeds the applet in the JSP file and downloads a plugin at the client-side.
index.jsp

<jsp:plugin type= "applet" code= "nameOfClassFile"  width=”400” height=”500” >
<jsp:fallback>
Error:Could not download the plugin
</jsp:fallback>
</jsp:plugin> 
You can also try this code with Online Python Compiler
Run Code

The above code is a sample code that will display an error message upon failure to download applet content.

Attributes

The JSP plugin action tag uses specific attributes that increase its functionality.

  • Type: Type of object the plugin will execute. It has no default parameter and must be set to either bean or applet.
  • Code: Name of the java class file along with the .class at the end.
  • Codebase: Absolute or a relative path of the directory where applet code exists.
  • Name: name of bean or applet instance.
  • Align: position of the applet.
  • Height: Signifies the height of the applet.
  • Width: Signifies the width of the applet.
  • jreversion: The version of JRE (Java Runtime Environment)
  • <jsp: params>: name and parameter value passed to applet.
  • <jsp: fallback>: a text message displayed if the applet plugin fails to load.

Example:

<jsp:plugin
  type="bean|applet"
  code="classFile"
  codebase="classFileDirectory"
   [ name="instancename" ]
   [align="bottom|top|middle|left|right"]
   [ height="displayPixels" ]
   [ width="displayPixels" ]
   [ jreversion="JREVersion" ]
  >
   [<jsp:params> 
<jsp:param name="parametername" value="parametervalue" />
   </jsp:params>]
  [<jsp:fallback>Failure Message</jsp:fallback>]
</jsp:plugin>

Displaying applet in JSP 

Let us consider a simple example that uses jsp:plugin to display an applet.To plugin an applet we need to create one first.Therefore here is an applet “ButtonMoveApplet.java”.

ButtonMoveApplet.java
import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.awt.event.*;


public class ButtonMoveApplet extends Applet {
  Button move;
  Random r;


  public void init() {
  setLayout(null);
  move = new Button("Click me");
  add(move);
  move.reshape(10,10,70,30);
  r = new Random();
  setBackground(Color.red);
  setForeground(Color.yellow);
  }
  public void paint(Graphics g){
  g.drawString("Welcome JSP-Applet",100,100);
  }
  public boolean action(Event evt, Object whatAction) {
 if (!(evt.target instanceof Button))
 return false;
   String buttonLabel = (String)whatAction;
 if (buttonLabel == "Click me") {
 move.reshape(Math.abs(r.nextInt())%(size().width-70),
 Math.abs(r.nextInt())%(size().height-30),70,30);
 repaint();
  }
  return true;
  }
}
You can also try this code with Online Java Compiler
Run Code

To use this applet, we need to plug it into our jsp file using the JSP plugin.

index.jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Mouse Move</title>
    </head>
    <body bgcolor="yellow">
<h1>Mouse Move Example</h1>
<h2> JSP PLUGIN </h2>
        <jsp:plugin type="applet" code="ButtonMoveApplet.class" 
  width="400" height="400">
  <jsp:fallback>
   <p>Unable to load</p>
   </jsp:fallback>
</jsp:plugin>


    </body>
</html>

It is important to note that the ButtonMoveApplet.class file has to reside in the same directory as the index.jsp file.

Output:

Steps for displaying applet in JSP

  • Create an applet file in .java format
  • Compile the applet file and save the new .class file in the same directory as the index.jsp file
  • Create the index.jsp file
  • Use the jsp: plugin action tag
  • Save the index.jsp file

FAQs

  1. What is the JSP applet plugin?
    A JSP applet plugin is an HTML action tag used to attach a java applet inside a jsp file. It downloads the plugin on the client-side, making the response time much faster.
     
  2. What is applet explain?
    An applet is essentially a small Java-based program that can run inside a browser with the help of a plugin.

Key Takeaways

In this article, we learned about Applet in JSP and how they are displayed on a browser with the help of a JSP plugin. We also learned through a practical example how the JSP plugin handles errors and uses its attributes to increase the functionality of the applet. However, this isn't enough, as there is always much more to explore and learn about this vast field of Web Development. To know more about JSPand its intricacies, check out the articles on JSP or enroll in our highly curated Web Development course.  

Live masterclass