jsp:forward standard action Tag

The <jsp:forward> action allows the request to be forwareded to anothr JSP, servlet, or a static resources.
Syntax of <jsp:forward> action tag
<jsp:forward page="URL" / >
or
<jsp:forward page="URL" > <jsp:param name="param name" value="param value" /> ....
</jsp:forward>
Example of <jsp:forward> action tag
index.jsp
<html> <head> <title>Welcome jsp:forward example </title> </head> <body> <form action="loginValidate.jsp" method="post"> <fieldset> <legend> <font face="Courier New" size="+1" color="red"> Login </font> </legend> <table> <tr> <td>User Name </td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td>Password </td> <td> <input type="text" name="password"> </td> </tr> <tr> <td colspan=2> <input type="submit" value="Login"> </td> </tr> </table> </fieldset> </form> </body> </html>
loginValidate.jsp
<%@ page language="java" %> <html> <head> <title>Login Validate page</title> </head> <body> <% String userName = request.getParameter("userName"); String password = request.getParameter("password"); if (userName != null && password != null && userName.equals("Sunil") && password.equals("Kumar")) { %> <jsp:forward page="welcome.jsp" /> <% } else { %> <jsp:include page="index.html"/> <% } %> </body> </html>
welcome.jsp
<%@ page language="java" %> <html> <head> <title>welcome page</title> </head> <body> <% String name=request.getParameter("userName"); out.println(name); %> </body> </html>
Output
jsp:forward
jsp:forward
Download this example
Example of <jsp:forward> action tag with parameter
index.jsp
<html> <head> <title>Welcome jsp:forward example with parameter </title> </head> <body> <form action="loginValidate.jsp" method="post"> <fieldset> <legend> <font face="Courier New" size="+1" color="red"> Login </font> </legend> <table> <tr> <td>User Name </td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td>Password </td> <td> <input type="text" name="password"> </td> </tr> <tr> <td colspan=2> <input type="submit" value="Login"> </td> </tr> </table> </fieldset> </form> </body> </html>
loginValidate.jsp
<%@ page language="java" %> <html> <head> <title>Login Validate page</title> </head> <body> <% String userName = request.getParameter("userName"); String password = request.getParameter("password"); if (userName != null && password != null && userName.equals("Sunil") && password.equals("Kumar")) { %> <jsp:forward page="welcome.jsp" > <jsp:param value="Login successful" name="user"/> </jsp:forward> <% } else { %> <jsp:include page="index.html"/> <% } %> </body> </html>
welcome.jsp
<%@ page language="java" %> <html> <head> <title>welcome page</title> </head> <body> <% String name=request.getParameter("user"); out.println(name); %> </body> </html>
Output
jsp:forward
jsp:forward
Download this example
 



 

Latest Tutorial


Most Visited Tutorial