Advertisement



< Prev
Next >



JSP forward action





JSP Action tags allow you to perform some actions on a JSP page and this action could be - 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>




 <jsp:forward page="URL" >  
<param name="parameterName1" value="valueOfParameter" > 
<param name="parameterName2" value="valueOfParameter" > 
...
 </jsp:forward> 





An example of using the <jsp:forward> action


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" /> 




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 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 JspForward.jsp web page, the information entered in the form passed to it by EnterData1.jsp is forwarded to another web page ShowData.jsp and a new parameter named message is added using the <jsp:forward> action element.


JspForward.jsp
<html>
<head>	
<title>Using JSP config object</title>
</head>

<body>

< jsp:forward page="ShowData.jsp" >
<jsp:param  name="message" value="Have a wonderful day"/>
<</jsp:forward%gt;

</body>
</html>




Next, web page ShowData.jsp is displayed with the data entered by the user in the form on EnterData1.jsp and the value of parameter added to the request using on the page JspForward.jsp, which is all accessed using the JSP's inbuilt request object.


ShowData.jsp
<html>
<head>	
<title>Using JSP config object</title>
</head>

<body>

<%
out.println("Username - " + request.getParameter("Username"));
out.println("<br/>");
out.println("City - " + request.getParameter("Cityname"));
out.println("<br/>");
out.println("Message - " + request.getParameter("message"));
%>

</body>
</html>



As you can see in the output windows shown below, user's inputs taken in a form on a web page EnterData1.jsp, the information entered is sent to the web page JspForward.jsp(specified in the action attribute of <form> tag).





the information entered in the form on EnterData1.jsp is forwarded to another web page JspForward.jsp, where 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.






Please share this article -




< Prev
Next >
< JSP taglib Directive
JSP include Action >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement