Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp Using Applet in JSP

Related Tutorials


 
 

Share on Google+Share on Google+

Using Applet in JSP

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 8, 2008

Related Tutorials

Discuss: Using Applet in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
sunil kumar chaurasia
February 2, 2012
Applet

This is absolutely very good site.Here all the example are very excellent.
viji
April 25, 2012
embed voice

Hi, I am working on a project which needs to give voice guidance. for ex: alert message should be spelt.if i click on submt button, it should say that form submitted.this project is mainly for blind people. Kindly do help me
kobena
May 6, 2012
how to include chat into JSP

I am a student I want to include chat in my JSP application. Please helpe me.
sathya
August 21, 2012
applet with jsp

this page is really useful for creating jsp project with the applet because give many features it features can be used
DMCA.com