In this tutorial you will learn about the JSP forward action tag and its application with practical example.
JSP forward action tag
This tag is used to terminates the action of the current page and forwards the request to another page which is bind with forward tag, it may be static page, JSP page, or Java Servlet.
Syntax without parameter:
1 |
<jsp:forward page="Relative URL" /> |
As we discussed in our previous article that param action tag we can use with forward tag and include tag, so let’s see how it will use in forward action tag.
Syntax with param:
1 2 |
<jsp:forward page="relativeURL | <%= expression %>"> <jsp:param name="parameterName" value="parameterValue | <%=expression%>" />/> |
Example : Let’s get understand it with simple example, In this example I did not use parameter action , we will do in next example.
#index.jsp: In this jsp we will declare our jsp:forward action tag and will set the path in “page” attribute. Check below code of this jsp page.
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Index Page</title> </head> <body> <jsp:forward page="forwardedPage.jsp" /> </body> </html> |
#forwardedPage.jsp: This jsp page will be appear instead of index.jsp page because we put this page in forward tag, check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Page</title> </head> <body> <h2>Welcome to Forward Action Tag</h2> <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %> </body> </html> |
Output: So output of above code will look like below image.
Example with param action tag : In the below example we will get understand about jsp:forward tag with jsp:param tag.
#index.jsp: The same page i have used, apply only jsp:param action tag, check below tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Index Page</title> </head> <body> <jsp:forward page="forwardedPage.jsp" > <jsp:param name="siteName" value="w3adda.com" /> </jsp:forward> </body> </html> |
#forwardedPage.jsp: In this page we will get our parameter like below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Page</title> </head> <body> <h2>Welcome to Forward Action Tag</h2> <% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %><br> <%= request.getParameter("siteName") %> </body> </html> |
Output: The output of the above code will look like below image.