JSP forward action tag is used for forwarding a request to the another resource (It can be a JSP, static page such as html or Servlet). Request can be forwarded with or without parameter. In this tutorial we will see examples of <jsp:forward>
action tag.
Syntax:
1) Forwarding along with parameters.
<jsp:forward page="display.jsp"> <jsp:param ... /> <jsp:param ... /> <jsp:param ... /> ... <jsp:param ... /> </jsp:forward>
2) Forwarding without parameters.
<jsp:forward page="Relative_URL_of_Page" />
Relative_URL_of_Page: If page is in the same directory where the main page resides then use page name itself as I did in the below examples.
Example 1: JSP Forward without passing parameters
In this example we are having two JSP pages – index.jsp and display.jsp. We have used <jsp:forward>
action tag in index.jsp
for forwarding the request to display.jsp
. Here we are not passing any parameters while using the action tag. In the next example we will pass the parameters as well to another resource.
index.jsp
<html> <head> <title>JSP forward action tag example</title> </head> <body> <p align="center">My main JSP page</p> <jsp:forward page="display.jsp" /> </body> </html>
display.jsp
<html> <head> <title>Display Page</title> </head> <body> Hello this is a display.jsp Page </body> </html>
Output:
Below is the output of above cpde. It is basically the content of display.jsp, which clearly shows that index.jsp didn’t display as it forwarded the request to the display.jsp page.
Example 2: JSP Forward with parameters
Here we are passing the parameters along with forward request. For passing parameters we are using <jsp:param>
action tag. In this example we are passing 4 parameters along with forward and later we are displaying them on the forwarded page. In order to fetch the parameters on display.jsp page we are using getParameter method of request implicit object.
index.jsp
<html> <head> <title>JSP forward example with parameters</title> </head> <body> <jsp:forward page="display.jsp"> <jsp:param name="name" value="Chaitanya" /> <jsp:param name="site" value="BeginnersBook.com" /> <jsp:param name="tutorialname" value="jsp forward action" /> <jsp:param name="reqcamefrom" value="index.jsp" /> </jsp:forward> </body> </html>
display.jsp
<html> <head> <title>Display Page</title> </head> <body> <h2>Hello this is a display.jsp Page</h2> My name is: <%=request.getParameter("name") %><br> Website: <%=request.getParameter("site") %><br> Topic: <%=request.getParameter("tutorialname") %><br> Forward Request came from the page: <%=request.getParameter("reqcamefrom") %> </body> </html>
Output:
Above code directly displayed display.jsp page, which is displaying the parameters passed from index.jsp page.
Pankaj says
can you explain the difference b/w include and forward action tag
Nikhil says
include is like circular linked list and forward is like linear linked list.
xywei says
yeah. your opinion is good!
Jomy Joseph says
how to redirect control from html page to jsp page using form action method