Using Applet in JSP

Ads
 

Using Applet in JSP

Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer.

Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer.

Using Applet in JSP

     

Example program which illustrates use of <jsp:plugin> tag to include Applet in JSP page

What is applet ?

Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining. For reading more detail on apple visit: http://www.roseindia.net/java/example/java/applet/

JSP-Applet

To use applet in JSP page we can use <jsp:plugin>. By the use of <jsp:plugin> you can include an applet and JavaBean in your JSP page. Syntax of <jsp:plugin> is :

<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>
   text message
   ..........
   </jsp:fallback>]
</jsp:plugin>

Attributes:

  1. type: this is the type of object plugin will execute. You have to specify it either bean or applet since it has no default value.
  2. code: name of the java class file. You must have to write ".class" with class file name.
  3. codebase: absolute or relative path of directory where applet code file exists.
  4. name: name of bean or applet instance.
  5. align: position of applet or bean display on the browser.
  6. height: as its name signifies, height of applet or bean display
  7. width: as its name signifies, width of applet or bean display
  8. jreversion: version of Java Runtime environment 
  9. <jsp:params>: name and parameters value passed to applet or bean.
  10. <jsp:fallback>: a text message to be displayed if applet plugin cannot be started.

To plugin an applet in JSP page we require an applet. So we have made an applet "ButtonMoveApplet.java". Code for this applet file is given as below:

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;
  }
}

To use this applet we have used <jsp:plugin> in AppletJsp.jsp. This applet is being used in JSP file by the use of following code.

....................
....................
<jsp:plugin type="applet" code="ButtonMoveApplet.class" 
  width="400" height="400">
  <jsp:fallback>
   <p>Unable to load applet</p>
   </jsp:fallback>
</jsp:plugin>
..................
..................

Full code for AppletJsp.jsp is gievn as below:

AppletJsp.jsp

<%@ page language="java" %>
<html>
<head>
<title>Welcome JSP-Applet Page</title>
</head>
<body>
<jsp:plugin type="applet" code="ButtonMoveApplet.class" 
  width="400" height="400">
  <jsp:fallback>
   <p>Unable to load applet</p>
   </jsp:fallback>
</jsp:plugin>
</body>
</html>

Steps for running this example:

  • Create and save ButtonMoveApplet.java
  • Compile and put its class file in your web folder(e.g Example in our case)
  • Create and save AppletJsp.jsp
  • Start tomcat server and type following URL in web browser:
    http://localhost:8080/Example/AppletJsp.jsp

You will get following output.

Output:

Download Source Code

Ads

Ads