JSP - Actions

JSP - Actions

  • These actions use constructs in XML syntax to control the behavior of the servlet engine. 
  • You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin.

  • There is only one syntax for the Action element, as it conforms to the XML standard.

Syntax:



    • jsp:include - Includes a file at the time the page is requested.
    • jsp:useBean - Finds or instantiates a JavaBean.
    • jsp:setProperty - Sets the property of a JavaBean.
    • jsp:getProperty - Inserts the property of a JavaBean into the output.
    • jsp:forward - Forwards the requester to a new page.
    • jsp:plugin - Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin.
    • jsp:element - Defines XML elements dynamically.
    • jsp:attribute - Defines dynamically-defined XML element's attribute.
    • jsp:body - Defines dynamically-defined XML element's body.
    • jsp:text - Used to write template text in JSP pages and documents.


Common Attributes

  • There are two attributes that are common to all Action elements: 
  • the id attribute and the scope attribute.


    • Id attribute
      • The id attribute uniquely identifies the Action element, and allows the action to be referenced inside the JSP page. 
      • If the Action creates an instance of an object, the id value can be used to reference it through the implicit object PageContext.


    • Scope attribute
      • This attribute identifies the lifecycle of the Action element. 
      • The id attribute and the scope attribute are directly related, as the scope attribute determines the lifespan of the object associated with the id. 
      • The scope attribute has four possible values: 
        • (a) page, 
        • (b)request, 
        • (c)session, and 
        • (d) application.

The Action

  • This action lets you insert files into the page being generated. 

Syntax:



  • page - The relative URL of the page to be included.
  • flush - The boolean attribute determines whether the included resource has its buffer flushed before it is included.


Example:
myDate.jsp
Today's date: <%= (new java.util.Date()).toLocaleString()%>

main.jsp



The Action

  • The useBean action is quite versatile. 
  • It first searches for an existing object utilizing the id and scope variables. 
  • If an object is not found, it then tries to create the specified object.

Syntax:


  • class - Designates the full package name of the bean.
  • type - Specifies the type of the variable that will refer to the object.
  • beanName - Gives the name of the bean as specified by the instantiate () method of the java.beans.Beans class.


The Action

  • The setProperty action sets the properties of a Bean. 
  • The Bean must have been previously defined before this action. 
  • There are two basic ways to use the setProperty action −


Syntax-1:

...


Syntax-2:

   ...
   



  • name- Designates the bean the property of which will be set. The Bean must have been previously defined.


  • property - Indicates the property you want to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
  • value - The value that is to be assigned to the given property. The the parameter's value is null, or the parameter does not exist, the setProperty action is ignored.
  • param - The param attribute is the name of the request parameter whose value the property is to receive. You can't use both value and param, but it is permissible to use neither.


The Action

  • The getProperty action is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output.
  • The getProperty action has only two attributes, both of which are required. 
  • The syntax of the getProperty action is as follows −



...



  • name - The name of the Bean that has a property to be retrieved. The Bean must have been previously defined.
  • property - The property attribute is the name of the Bean property to be retrieved.


Example
TestBean.java
package action;

public class TestBean {
   private String message = "No message specified";

   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}
main.jsp

in body tag
         
                     value = "Hello JSP..." />
           
         

The Action

  • The forward action terminates the action of the current page and forwards the request to another resource such as a static page, another JSP page, or a Java Servlet.

Syntax:



  • page - Should consist of a relative URL of another resource such as a static page, another JSP page, or a Java Servlet.


Example:
myDate.jsp
Today's date: <%= (new java.util.Date()).toLocaleString()%>

main.jsp
in body tag




The Action

  • The plugin action is used to insert Java components into a JSP page.
  •  It determines the type of browser and inserts the or tags as needed.
  • If the needed plugin is not present, it downloads the plugin and then executes the Java component. 
  • The Java component can be either an Applet or a JavaBean.


  • The plugin action has several attributes that correspond to common HTML tags used to format Java components. 
  • The element can also be used to send parameters to the Applet or Bean.


  • Example:

   width = "60" height = "80">
   
   

   
      Unable to initialize Java Plugin
   




The Action


  • The action can be used to write the template text in JSP pages and documents. 

Syntax:

Template data

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form