JSP Action Tags – jsp useBean, jsp include, jsp forward | What are Action Tags in JSP? | List of JSP Action Tags

In this tutorial, we will discuss JSP Action Tags – JSP useBean, JSP include, JSP forward, and JSP param in detail with an example. First and foremost, let’s understand what is JSP Action Tags. In JSP, to use the bean, we need Action Tags. Also, know what is the difference between Directives & Actions along with the ultimate list of Action Tags in JSP.

This JSP Action Tags – JSP useBean, JSP include, JSP forward, and JSP param Tutorial Includes:  

What is JSP Action?

The tags which are used to implement the bean in a JSP are called JSP Action tags. JSP action tags are a set of predefined tags given by the JSP container to do some common tasks thus reducing the Java code in JSP.

A few of the common tasks are listed below:

  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.

Directives vs Actions

  • Directives are applied while the translation phase whereas Actions are utilized at the time of the request processing phase.
  • Unlike Directives, Actions are re-evaluated each time the page is accessed.

List of some Action Tags in JSP

The following are some of the Action Elements used in JSP:

JSP Action Tags Description
jsp:forward forwards the request and response to another resource.
jsp:include includes another resource.
jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean objects.
jsp:getProperty prints the value of the property of the bean.
jsp:plugin embeds another component such as applet.
jsp:param sets the parameter value. It is used in forward and includes mostly.
jsp:fallback can be used to print the message if a plugin is working. It is used in jsp:plugin.

Do Refer:

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 only response.

Attributes:

S.No. Attribute & Description
1. page The relative URL of the page to be included.
2. flush The boolean attribute determines whether the included resource has its buffer flushed before it is included.

Syntax:

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

Example:

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

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

2. <jsp:forward> Action

The JSP 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 a static page, another JSP page, or a java servlet.

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. It implements the same functionality as 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"/>

Example:

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

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

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 is used within any other action besides the ones just mentioned.

Syntax:

 <jsp:paramname="parametername" value = "parametervalue" />

Example:

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

JavaBeans class:

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

4. <jsp:useBean> Action

It is the standard action, 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 determined and with a newly declared scripting variable, It is given an Id. When an 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

  • id: It is used to identify the object instance in the specified scope.
  • scope: The scope in which the reference to the bean is available.
  • class: a fully qualified class name.
  • bean name: The name of the bean, as provided to the instantiate() method of the java.beans.Bean class.
  • 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"/>

Example: 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Action JSP1</title>
</head>
<body>
<jsp:useBean id="name" class="demotest.DemoClass">
</body>
</html>

5. <jsp:setProperty> Action

A bean property is set by this property. Using the standard action, the bean must have been previously defined. you can set a bean property by using it in the following ways:

  • using a parameter in the request object.
  • using a String constant value.
  • 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 convert it to a string and finally insert it into the output. The property value is converted into a String within the 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, JSP setProperty, and JSP getProperty:

JavaBean class:

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

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

Leave a Comment