action tags of jsp

In this post, you will learn:

  • What are action tags in JSP?
  • What is the role of action tags in the auto generated servlet?
  • Use of include action of JSP.

The Action tags of JSP:

Action tags in JSP facilitate automation of common operations such as creation of objects, setting objects’ properties, writing objects’ property values to the output stream, including the contents of a component to the response of current request and forwarding request to another component etc. An action tag has following syntax:

<jsp:actionName attribute=value,.. />

Commonly used action tags of JSP are:

ActionDescription
<jsp:useBean />It is used to create a Java Bean object and to save it in a scope or to obtain an existing Java Bean object from a scope.
<jsp:setProperty />It is used to set the properties of a Java Bean object.
<jsp:getProperty />It is used to write the value of a Java Bean property to the output stream.
<jsp:include />It is used to include the contents of the specified page to the response of current request.
<jsp:forward />It is used to forward the request to the specified page.
<jsp:param />It is used to define parameters to be provided to the included or forwarded page.

At the time of translation, for each action tag, statements are generated in the _jspService() method of the auto generated servlet to perform the task represented by the action. For example: if a JSP page has <jsp:include /> action then in the _jspService() method a call to include() method of the RequestDispatcher is generated to include the contents of the specified page to the response.

Practical example of <jsp:include /> action:

This action can be used in the following two ways:

1. Without parameters.

<jsp:include page=”url of the page to be included” />

2. With parameters.

<jsp:include page=”url of the page to be included” >

<jsp:param name=”parameter name” value=”parameter value” />

</jsp:include>

First, the index.html :

<form method="post" action="adder.jsp">
First No. <input type="text" name="num1"><br/>
Second No. <input type="text" name="num2"><br/>
<input type="submit" value="add">
</form>

Second, the adder.jsp :


<!-- Scriptlet tag starts. -->
<%
/* Value of the request parameters is read. */
int a=Integer.parseInt(request.getParameter("num1"));
int b=Integer.parseInt(request.getParameter("num2"));
/* sum is calculated. */
int c=a+b;
%><!-- Scriptlet tag ends. -->

<!-- sum is written to the response through expression tag. -->
<b>Sum is: </b> <%=c%> 
<hr/>
<!-- contents of index.html are included to the response. -->
<jsp:include page="index.html" />

The response of adder.jsp contains the sum of given numbers as well as the input form so that further request can be sent to it to add another numbers.

Practical use of the other actions will be covered in the upcoming posts.


Its time to recap the main concepts:

  1. Action tags are used to automate common operations of the request processing.
  2. For each action tag of JSP, a statement or statements are generated in the _jspService() method of the auto generated servlet to perform the task represented by the action.
  3. Action tags can’t be nested in the scriptlet or declaration tag.
  4. In a JSP page, any number of action tags can be used.
If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

One thought on “action tags of jsp

Leave a Reply

Your email address will not be published. Required fields are marked *