Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | jsp:plugin Action - JavaServer Pages (JSP) Standard Actions

Chapter: Internet & World Wide Web HOW TO PROGRAM - JavaServer Pages (JSP): Bonus for Java™ Developers

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

jsp:plugin Action - JavaServer Pages (JSP) Standard Actions

Figure 31.14 defines an applet that draws a picture using the Java2D API.

3. <jsp:plugin> Action

 

Action <jsp:plugin> adds an applet or JavaBean to a Web page in the form of a brows-er-specific object or embed XHTML element. This action also enables the client to download and install the Java Plug-in if it is not already installed. Figure 31.13 describes the attributes of action <jsp:plugin>.

 

Figure 31.14 defines an applet that draws a picture using the Java2D API. The applet has three parameters that enable the JSP implementor to specify the background color for the drawing. The parameters represent the red, green and blue portions of an RGB color with values in the range 0–255. The applet obtains the parameter values in lines 21– 23. If any exceptions occur while processing the parameters, the exceptions are caught at line 32 and ignored, leaving the applet with its default white background color.

 



 

    // Fig. 10.14: ShapesApplet.java

    // Applet that demonstrates a Java2D GeneralPath.

    package com.deitel.advjhtp1.jsp.applet;

4

    // Java core packages

    import java.applet.*;

    import java.awt.event.*;

    import java.awt.*;

    import java.awt.geom.*;

 

      // Java extension packages

      import javax.swing.*;

13

14  public class ShapesApplet extends JApplet {

 

15

      // initialize the applet

      public void init()

      {

      // obtain color parameters from XHTML file

try {

             int red = Integer.parseInt( getParameter( "red" ) );

             int green = Integer.parseInt( getParameter( "green" ) );

             int blue = Integer.parseInt( getParameter( "blue" ) );

            

             Color backgroundColor = new Color( red, green, blue );

     setBackground( backgroundColor );

 }

      // if there is an exception while processing the color

      // parameters, catch it and ignore it

      catch ( Exception exception ) {

      // do nothing

      }

      }

      public void paint( Graphics g )

      {

      // create arrays of x and y coordinates

      int xPoints[] =

      { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };

int yPoints[] =

{ 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };

      // obtain reference to a Graphics2D object

      Graphics2D g2d = ( Graphics2D ) g;

      // create a star from a series of points

      GeneralPath star = new GeneralPath();

      // set the initial coordinate of the GeneralPath

star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );

 

             // create the star--this does not draw the star

             for ( int k = 1; k < xPoints.length; k++ )

             star.lineTo( xPoints[ k ], yPoints[ k ] );

            

             // close the shape

             star.closePath();

            

             // translate the origin to (200, 200)

             g2d.translate( 200, 200 );

 

             // rotate around origin and draw stars in random colors

             for ( int j = 1; j <= 20; j++ ) {

             g2d.rotate( Math.PI /            10.0 );

                                   

             g2d.setColor(                      

             new Color( ( int )      ( Math.random() * 256 ),

             ( int )  ( Math.random() * 256         ),

             ( int )  ( Math.random() * 256         ) ) );

                                   

             g2d.fill( star );   // draw a filled star           

             }

             }

             }

 

Fig. 31.14  An applet to demonstrate <jsp:plugin> in Fig. 31.15

 

 

Most Web browsers in use today do not support applets written for the Java 2 platform. Executing such applets in most of today’s browsers requires the Java Plug-in. Figure 31.15 uses the <jsp:plugin> action (lines 10–22) to embed the Java Plug-in. Line 11 indi-cates the package name and class name of the applet class. Line 12 indicates the code-base from which the applet should be downloaded. Line 13 indicates that the applet should be 400 pixels wide and line 14 indicates that the applet should be 400 pixels tall. Lines 16–20 specify the applet parameters. You can change the background color in the applet by changing the red, green and blue values. Note that the <jsp:plugin> action requires any <jsp:param> actions to appear in a <jsp:params> action.

 

             <!-- Fig. 10.15: plugin.jsp -->

            

             <html>

            

             <head>

             <title>Using jsp:plugin to load an applet</title>

             </head>

 

             <body>

             <jsp:plugin type = "applet"

             code = "com.deitel.advjhtp1.jsp.applet.ShapesApplet"

             codebase = "/advjhtp1/jsp"

             width = "400"

             height = "400">

                       

             <jsp:params>           

             <jsp:param name = "red" value = "255" />         

             <jsp:param name = "green" value = "255" />     

             <jsp:param name = "blue" value = "0" />

             </jsp:params>          

                       

             </jsp:plugin>

             </body>

             </html>



Fig. 31.15 Using <jsp:plugin> to incorporate a Java applet into a JSP

 

To test the <jsp:plugin> action in Tomcat, copy plugin.jsp and ShapesAp-plet.class into the jsp directory created in Section 31.3. [Note: ShapesApplet is defined in package com.deitel.advjhtp1.jsp.applet. This example will work only if the proper package directory structure is defined in the classes directory.] Open your Web browser and enter the following URL to test plugin.jsp:

 

http://localhost:8080/advjhtp1/jsp/plugin.jsp

 

The screen captures in Fig. 31.15 show the applet executing in Microsoft Internet Explorer 5.5 and Netscape Navigator 6.0.

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.