alt
Advertisement
Sponsored links
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow JSP arrow JSP Action tags

There are 22 related tutorials for the topic action tag in jsp you are looking for.

Site Search


JSP Action tags

JSP Action tags

In this section let us see about Action tag which is another type of tag, forward action tag and useBean action tag used in Java Server pages with syntax, usage, example and explanation in detail.

Action Tag:

Action tag 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.

General syntax of Action Tag: 


<jsp:action attributes />

In the above statement jsp is a keyword.

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

  • include
  • forward
  • useBean

Syntax, attributes and usage of above three action tags are provided in brief.

include action tag:

include is a type of directive tag. include tag has the same concept as that of the include directive. The include tag is used to include either static or dynamic content, wherever required.

General syntax of include action Tag:  


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

In the above statement, the words jsp, include, flush and page are keywords. The include action tag includes a static file or it is used to send a request to a dynamic file. If the included file is a static file, the contents are included as such in the called JSP file. In contrast, if the included file is dynamic, then a request sends to the dynamic file. Once the include action is completed, the result is sent back.

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>
 

The file is dynamic and extra attribute clause passed is jsp:param clause, and this is used to pass the name and value of a parameter to the dynamic file. In the above, the words jsp, include, flush, page, param, name and value are keywords. In the above syntax for dynamic file inclusion, the name attribute specifies the parameter name and its case-sensitive literal string. The value attribute specifies the parameter value, taking either a case-sensitive literal string or an expression that is evaluated at request time.

In the syntax of include action Tag we relative URL placed within " " denotes the location of the file to be included or denotes the pathname to be included. This can also be an expression which is taken automatically as string, denoting the relative URL.

NOTE: the pathname must not contain the protocol name, port number, or domain name. Both absolute and relative representations can be made for denoting the URL.

The flush attribute can only take on value "true" as mentioned in syntax notation.

NOTE: Do not to include the value if false for the flush attribute.

The syntax used for including dynamic file had <jsp:param> clause allows the user to pass one or more name/value pairs as parameters to an included file. This is the syntax for dynamic file, meaning that the included file is a dynamic file (i.e. either a JSP file or a servlet, or other dynamic file). Also, the user can pass more than one parameter to the included file by using more than one <jsp:param> clause.

For example:

An example to include a static file exforsys.html in the include action tag: 


   
 <jsp:include page="exforsys.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="example/test.jsp">
<jsp:include name="username" value="exforsys" />

</jsp:include>

Let us see about the other two commonly used action tags namely forward action tag and useBean action tag with syntax, usage, example and explanation in detail

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.

NOTE: The target file must be in the same application context as the forwarding JSP file.

General syntax of forward action Tag:  


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

Another syntax representation for forward action Tag is as below: 


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

                   value
="parameterValue" | <%= expression %>}" />

      </jsp:forward>
 

In the above statement, jsp, forward, page, param, name and value are all keywords. The relative URL of the file to which the request is forwarded is represented as a String, an expression or as absolute or relative representation to the current JSP file denoting the pathname.

NOTE: Do not include protocol name, port number, or domain name in the pathname. The file can be a JSP file or a servlet, or any other dynamic file.

If the user follows the second syntax representation of forward action Tag, then the target file should be dynamic where jsp:param> clause is used. This allows the sending of one or more name/value pairs as parameters to a dynamic file. The user can pass more than one parameter to the target file by using more than one <jsp:param> clause. In the above second representation, the name attribute is used to specify the parameter name and its case-sensitive literal string. The value attribute can be specified by the parameter value taking either a case-sensitive literal string or an expression that is evaluated at request time.

For example: 


      <jsp:forward page ="exforsys.html" />
      <
jsp:forward page ="/example/test" />
      <
jsp:param   name ="username" 

                   value
="exforsys" />

      </jsp:forward>
 

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.

General syntax of useBean action Tag: 


<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>
}

 

 The <jsp:useBean locates the instance of the bean. If the bean is not present, then the <jsp:useBean> instantiates it from a class.

In the above syntax of useBean action Tag there are several attributes mentioned. They are:

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.


Trackback(0)
Comments (0)add comment

Write comment

busy
 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape