JSP Action tags allow you to perform some actions on a JSP page and this action could be -
Forwarding the HTTP request to another JSP webpage.
Including the content of another JSP webpage within a JSP webpage.
Creating a Java bean(object) and using it to access its getter and setter methods and set the value of its instance variables.
Let us discuss the first JSP action tag, <jsp:forward>
<jsp:forward> Action Tag
JSP provides an action element, <jsp:forward>, which you can use to forward the HTTP requests to another page.
Advertisement
Syntax of <jsp:forward>
Up next, you are going to see a shortcut syntax of <jsp:forward> action element, where the HTTP request is forwarded to the page pointed by the URL.
<jsp:forward page="URL" />
Next below, you will see an another syntax of<jsp:forward> action element which does the same thing as previous syntax but it's just a longer version.
<jsp:forward page="URL" >
</jsp:forward>
You could even pass one or more parameters with the request that is being forwarded to another page using
the following version of <jsp:forward> action tag.
In the upcoming example, we are going to forward the client request to another web page using the shortcut syntax of <jsp:forward> action, such as -
<jsp:forward page="URL" />
First we are going to take user's inputs in a form on a web page EnterData1.jsp and as soon as the user press the submit button on the form, the information entered will be
sent over to the web page JspForward.jsp(specified in the action attribute of <form> tag).
EnterData1.jsp
<html>
<head>
<title>Using JSP config object</title>
</head>
<body>
<form action="JspForward.jsp">
Name : <input type ="text" name="Username">
<br/>
City : <input type="text" name="Cityname">
<input type ="submit" value ="submit">
</form>
</body>
</html>
Next, in the JspForward.jsp web page, the information entered in the form passed to it by the EnterData1.jsp
is forwarded to another web page ShowData.jsp,
and a new parameter named message is added by using the <jsp:forward> action element.
Next, the web page ShowData.jsp is displayed with the data entered by the user in the form on EnterData1.jsp,
and the value of parameter message, which was added to the request
using on the page JspForward.jsp, is accessed using the JSP's inbuilt request object.
As you can see in the output windows shown below, user's inputs taken in a form on a web page EnterData1.jsp. The entered
information is sent
to the web page JspForward.jsp(specified in the action attribute of <form> tag), when the submit button is clicked.
On the web page JspForward.jsp,
a new parameter is appended to the request using the
<jsp:forward> action and forwarded to ShowData.jsp, where all this information is displayed by using the forwarded JSP request object.