JSP Action Tags - jsp useBean, jsp include, jsp forward

In the previous tutorial, we have learned Expression language in JSP. In this tutorial, we will learn about JSP Action Tags - JSP useBean, JSP include, JSP forward, and JSP param in detail with an example. But first, understand what is JSP Action Tags. In order to use the bean, we need JSP Action Tag.

What are JSP Action Tags?

In order to use the bean in a JSP, we need some tags, those tags are called JSP Action tags. JSP action tags are a set of predefined tags provided by JSP container to perform some common tasks thus reducing the Java code in JSP.
Some Common tasks are:
1. Instantiate java bean object.
2. setting values to beans.
3. Reading values from beans.
4. Forwarding the requests to another resource.
5. Including another resource.

List of some JSP Action Tags:

1. <jsp:include> Action:

It is used for both static and dynamic resources on a JSP page. The resources must be in the same context as the current page. It includes the output of the included page during runtime. It doesn't include the content of the page, it includes an only response. It has two attributes:                
a) page: it defines the resources that will be included. The URL is interpreted relative to the current JSP page.
b) flush: It is an optional boolean attribute. if true, the buffer is flushed prior to the inclusion.

Syntax : <jsp:include page="page name" />


jsp action tags

jsp action tags

2. <jsp:forward> Action:

The forward action tag is used to forward the content of one JSP page to another JSP page. The forward action terminates the action of the current page and forwards the request to another resource such as static page, another JSP page, or a java servlet. The static or dynamic resource to which control has to be transferred is represented as URL. The user can have target file as an HTML file, another JSP file or a servlet. It provides the same functionality of the forward() method in the RequestDispatcher interface. It is used for forwarding the request from one JSP page to another JSP page.

syntax : <jsp:forward page ="URL"/>


jsp action tags

jsp action tags

3. <jsp:param> Action:

The JSP param action is used to add the specific parameter to the current request.Using a name/value pair this is done.The JSP:param action tag can be used inside a jsp:include, jsp:forward and jsp:plugin. A translation error will occur if <jsp:param> is used within any other action besides the ones just mentioned.
Syntax : <jsp:paramname="parametername" value = "parametervalue" />


jsp action tags

                        JavaBeans class:
jsp action tags

4. <jsp:useBean> 

It is the standard action <jsp:useBean>, which can make a java object available for use. Then it is possible to get and set the properties on the object(JavaBean). Within a given scope, the bean is defined and with a newly declared scripting variable, It is given an Id. When a <jsp:useBean>                element is used then no java scripting variables are created, instead, an EL variable is created. It is possible to use a variety of attributes with


a) id: It is used to identify the object instance in the specified scope.
b) scope: The scope in which the reference to the bean is available.
c) class: a fully qualified class name.
d) beanName: The name of the bean, as provided to the instantiate() method of the java.beans.Bean class.
e) type: It is an optional attribute that defines the type of the scripting variable defined.

Syntax: <jsp:useBean id="name" class= "package.class"                                              scope="page/request/session/application"/>

5. <jsp:setProperty> Action: 

A bean property is set by this property. Using the <jsp:useBean> standard action, the bean must have been previously defined.you can set a bean property by using <jsp:useBean> in the following ways:
a) using a parameter in the request object.
b) using a String constant value.
c) using a value from a request-time attribute

Syntax : <jsp:setProperty name= "propertyname"  property="some property"                 value =" some value" />

6. <jsp:getProperty> Action:

The getProperty action tag is used to retrieve the value of a given property and converts it to a string and finally insert into the output. The property value is converted into a String within the <jsp:getProperty> action in one of the two ways: The toString() method is called if the property is an object. The valueOf() method of the wrapper class is called if the property is primitive.

Syntax : <jsp:getProperty name="name" property="propertyname" />

Example using JSP useBean, setProperty and getProperty

                    JavaBean class:

jsp action tags

jsp action tags

You may also like:
JSP Action Tags - jsp useBean, jsp include, jsp forward JSP Action Tags - jsp useBean, jsp include, jsp forward Reviewed by Prashant Srivastava on December 19, 2018 Rating: 5

No comments:

Powered by Blogger.