Standard Action jsp:plugin

                                       Jsp:plugin

 << - PRIOR                 INDEX                 NEXT  ->>

 Syntax

<jsp:plugin
          type="bean|applet" 
          code="classFileName" 
          codebase="classFileDirectoryName"
</jsp:plugin>

Description

1.     The <jsp:plugin> tag is replaced by either an <object> or <embed> tag, whichever is most appropriate for the client Web browser (the <object> tag is for browsers that use HTML 4.0).
2.     The <jsp:params> element sends parameter names and values to an applet or Bean at startup.
3.     The <jsp:fallback> element provides a message for the user if the plugin does not start. If the plugin starts but the applet or Bean does not, the plugin usually displays a popup window explaining the error to the user.

Attributes:

1.     type="bean|applet"
The type of object the plugin will execute. You must specify either bean or applet, as this attribute has no default value.
2.     code="classFileName"
The name of the Java class file that the plugin will execute. You must include the .class extension in the name following code. The filename is relative to the directory named in the codebase attribute.
3.     codebase="classFileDirectoryName"
The absolute or relative path to the directory that contains the applet's code. If you do not supply a value, the path of the JSP file that calls <jsp:plugin> is used.
4.     name="instanceName"
A name for the Bean or applet instance, which makes it possible for applets or Beans called by the same JSP file to communicate with each other.

Example:

pluginstandardaction.jsp

<html>
<title> Plugin example </title>
<body bgcolor="white">
<h3> The given below applet is imported to this file : </h3>
<jsp:plugin type="applet" code="Pluginexample.class" codebase="applet"
 height="300" width="300">
<jsp:fallback>
Plugin tag not supported by browser.
</jsp:fallback>
</jsp:plugin>
<h4><font color=red>
The above applet is loaded using the Java Plugin from a jsp page using the
plugin tag.
</font>
</h4>
</body>
</html>

Pluginexample.java (Applet)

import java.awt.*;
import java.applet.*;
public class Pluginexample extends Applet
{
// Specify variables that will be needed everywhere, anytime here
// The font variable
Font bigFont;
// The colors you will use
Color redColor;
Color weirdColor;
Color bgColor;
public void init()
{
// Here we will define the varibles further
// Will use Arial as type, 16 as size and bold as style
// Italic and Plain are also available
bigFont = new Font("Arial",Font.BOLD,16);
// Standard colors can be named like this
redColor = Color.red;
// lesser known colors can be made with R(ed)G(reen)B(lue).
weirdColor = new Color(60,60,122);
bgColor = Color.blue;
// this will set the backgroundcolor of the applet
setBackground(bgColor);
}
public void stop()
{

}
// now lets draw things on screen
public void paint(Graphics g)
{
// tell g to use your font
g.setFont(bigFont);
g.drawString("PLUGIN example",80,20);
// Now we tell g to change the color
g.setColor(redColor);
// This will draw a rectangle (xco,yco,xwidth,height);
g.drawRect(100,100,100,100);
// This will fill a rectangle
g.fillRect(110,110,80,80);
// change colors again
g.setColor(weirdColor);
// a circle (int x, int y, int width, int height,int startAngle, int arcAngle);
// ovals are also possible this way.
g.fillArc(120,120,60,60,0,360);
g.setColor(Color.yellow);
// Draw a line (int x1, int y1, int x2, int y2)
g.drawLine(140,140,160,160);
// reset the color to the standard color for the next time the applets paints
// an applet is repainted when a part was'nt visible anymore
// happens most often because of browser minimizing or scrolling.

g.setColor(Color. black);}

}

Output:




 << - PRIOR                 INDEX                 NEXT  ->>

JSP Standard Actions Intraduction

STANDARD ACTIONS

        << - PRIOR                 INDEX                 NEXT  ->>

Introduction:

1.     JSP actions are special XML tags which control the behavior of servlet engine.
2.      JSP actions allow you to insert a file dynamically, reuse external Java Bean components, forward the request to the other page and generate HTML for Java Applet plug-in.




Action Element
Description
<jsp:useBean>
Enables JavaBeans components in that page
<jsp:getProperty>
Get a property value from JavaBeans component and adds to the response
<jsp:setProperty>
Sets the JavaBeans property value
<jsp:include>
Includes the response from the jsp page while processing.
<jsp:forward>
Forwards the processing of a request to ajsp page
<jsp:param>
Adds a parameter value to a request handed off using the <jsp:include or <jsp:forward>
<jsp:plugin>
Generates a HTML elements appropriate to a browser to execute an applet using Java Plugin.
<jsp:attribute>
Sets the value of the action element based on the body of this element.
<jsp:body>
Sets action element body based on the body of this element.
<jsp:element>
Dynamically generates XML element.
<jsp:text>
Encapsulates template text, needed in JSP pages written in XML.

































        << - PRIOR                 INDEX                 NEXT  ->>










                                     



Application scope in jsp with example

                                        APPLICATION SCOPE IN JSP                                     

     << - PRIOR                 INDEX                 NEXT  ->>

Application Scope

1.     We can maintain this scope in servlet by using ServletContext object.  But in jsp’s by using application implicit object.
2.     The information stored in application scope is by default available for all web applications in the tomcat .
3.     application scope will be started at the time of context object creation  and ends at the time of context object destroy  .
4.     ServletContext  interface defines the following methods to perform  attribute management in application scope.

Methods:

§  public void      setAttribute (String name, Object value)
§  public  Object  getAttribute (String name)
§  public  void      removeAttribute (String name)
§  public  Enumeration getAttributeNames ( )

     << - PRIOR                 INDEX                 NEXT  ->>




SESSION SCOPE IN JSP WITH EXAMPLE

                                        SESSION SCOPE IN JSP

     << - PRIOR                 INDEX                 NEXT  ->>
                               
Session Scope
1.     In Servlets this scope is maintained by HttpSession object, but in in Jsp maintained by session implicit object.
2.     Session scope will be started at the time of session object creation and ends at the time of session object destroy.
3.     The information is stored in session scope is by default available  to all servlets and  jsps available in that web application.
4.     HttpSession   interface defines the following methods to perform attribute management in session scope.

Methods:

§  public void      setAttribute (String name, Object value)
§  public  Object  getAttribute (String name)
§  public  void      removeAttribute (String name)
§  public  Enumeration getAttributeNames ( )

     << - PRIOR                 INDEX                 NEXT  ->>




JSP Scopes: page scope

                                   Jsp Scopes


     << - PRIOR                 INDEX                 NEXT  ->>

Introduction: 
                                              
In Servlets we have the following 3 scopes for storing information in the form of attributes.

1.     Request Scope
2.     Session Scope
3.     Application Scope

In addition to these 3 scopes we have page scope is available  in jsp’s.

                                                Page Scope
                                               ==========

1.     This scope is applicable only for jsp’s but not for servlet.
2.     This scope is maintained by pageContext implicit object.
3.     An object with this scope can be accessed by invoking the getAttribute ( ) method on the implicit page Context object. The object is created and destroyed for each client request to the page.
4.      This is the default scope for objects used with the jsp:useBean action.
5.     The JSP object can be accessed only from within the same page where it was created. JSP implicit objects out, exception, response, pageContext, config and page have page scope.
6.     Page scope in the most commonly used scope to share information between  tag handler class.
PageContext class defines the following methods to perform attribute management in page scope.

Methods:

1.     public void setAttribute (String name, Object value);
2.     public void getAttribute (String name);
3.     public void remove Attribute (String name);

Attribute Management in any scope:

PageContext class defines the following methods to perform attribute management in any scope.
1.     public void setAttribute (string name, object value, int scope);

Here scope value must be 1 to 4

PageContext.PAGE-SCOPE                 =    1
PageContext.REQUEST-SCOPE          =    2
PageContext.SESSION-SCOPE            =    3
PageContext.APPLICATION-SCOPE  =    4

Example:

setAttribute(“uname”,”chamu0001”,1)

2.     public Object getAttribute (String name, int scope);
3.     public void removeAttribute (String name, int scope);
4.     public Enumeration getAttribute NamesInScope(int scope);
5.     public Object findAttribute (String name);
First it will search in page scope for desired attribute, if it is not available then it will search in request scope followed by session scope and application scope.
page – request – session – application.


Example: UsingBeanScopePage.java

package Mybean;
public class UsingBeanScopePage{
  private static int counter = 0;
  public void setCounter(int counter) {
    this.counter = counter;
  }
  public int getCounter(){
    return counter;
  }
}

Index.jsp

<html>
  <head>
    <title>Using Beans and Page Scope</title>
  </head>
  <body>
    <h1>Using Beans and Page Scope</h1>
    <jsp:useBean id="pageScopeBean" class="Mybean.UsingBeanScopePage" scope="page" />
    <%
    pageScopeBean.setCounter(pageScopeBean.getCounter() + 1);
    %>
        Counter value is <%= pageScopeBean.getCounter() %>
  </body>
</html>

 Output:

Using Beans and Page Scope

couter value is 3



     << - PRIOR                 INDEX                 NEXT  ->>