Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: c exception error jsp io count page using invoke js handle if to ci counter e directive can pe ce

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

this is very useful for me

Posted by naga on Wednesday, 09.17.08 @ 18:05pm | #80098

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.