Books Related to Java, Core Java, Jquery, Web Services, SCJP, Spring, Hibernate and J2ME

Sunday, January 25, 2009

JSP Action Tags

This is the another type of tag, it is used to transfer the control between

pages and is also used to enable the use of server side JavaBeans. Instead of
using Java code, the programmer uses special JSP action tags to
either link to a Java Bean set its properties, or get its
properties.

Syntax of Action Tag: "<jsp:action attributes />"

There are so many action tags available for Java Server Pages.
The most commonly used action tags are three of them that are:


  1. include

  2. forward

  3. useBean

1. Include Action Tag:- It has the same concept as that of the

include directive. The include tag
is used to include either static or dynamic content, wherever required.

Syntax of include action Tag: <jsp:include page="{relativeURL | <%=

expression %>}" flush="true" />


If the file is dynamic one can use the syntax as below:

<jsp:include page="{relativeURL | <%= expression %>}" flush="true" />

<jsp:param name="parameterName" value="{parameterValue
| <%= expression %>}" />

</jsp:include>

For example: An example to include a static file jsp-directives.html

in the include action tag:

<jsp:include page="jsp-directives.html"

flush="true"/>

Let us see an example of dynamic file inclusion in the include action tag

which is done as below:

<jsp:include page="test/jsp-directives.html.jsp">

<jsp:include name="username" value="sandeep" />

</jsp:include>


2. Forward Action Tag:- The forward action tag is used to transfer
control to a static or dynamic resource. The static or dynamic resource to which
control has to be transferred is represented as a URL. The user can have the
target file as an HTML file, another JSP file, or a servlet. Any of the above is
permitted.

Syntax of forward action Tag: &nbsp;

<jsp:forward page="{relativeURL | <%= expression %>}" />


Now another representation for forward action Tag is as:

<jsp:forward page ="{relativeURL | <%= expression %>}" />

<jsp:param name ="parameterName" value="parameterValue"
| <%= expression %>}" />

</jsp:forward>

3. useBean Action Tag:- The useBean action tag is the most commonly

used tag because of its powerful features. It allows a JSP to create an instance
or receive an instance of a Java Bean. It is used for creating or instantiating
a bean with a specific name and scope. This is first performed by locating the
instance of the bean.

Syntax of useBean action Tag is:

<jsp:useBean id="beanInstanceName" scope="page|request|session|application"

{

class="package.class" | type="package.class"
|

class="package.class" type="package.class" |

beanName="{package.class | <%=
expression %>}"

type="package.class"

}

{ /> |

> other elements

</jsp:useBean>

}


There are the several attributes available in useBean Action Tag, that is:


  • id: This refers to a variable that is case sensitive, identifying
    the bean within scope attribute defined.

  • scope: This refers to the scope of the existence of bean. The
    default scope is page.

  • class: The class name is mentioned and using this bean is
    Instantiated. The data type to which bean must be assigned is mentioned in
    type.

  • beanName: When this is specified, the bean is instantiated by the
    java.beans.Beans.instantiate method.



No comments:

Post a Comment