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:
Commonly used action tags of JSP are:
|<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:
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.