Forward tag in JSP forwards request to another source. Another source can be anything like HTML file, Servlet or anothe JSP file.
The forward action terminates the action of the current page and forwards the request to another resource such as a static page, another JSP page, or a Java Servlet.
General syntex of JSP Forward Tag look like this:
Syntex:
<jsp:forward page="xyz"/>
JSP Forward Tag Example:
<jsp:forward page="Relative URL" />
Explanation: As shown above, forward tag in JSP has only one attributes “page”.
page Attribute: page attribute in JSP Forward Tag specifies the relative path of the source where request is going to be handled. This file can be any html, servlet or jsp file.
Example:
Let us reuse following two files
(a) header.jsp and
(b) index.jsp
Following is the content of index.jsp file:
<html> <head> <title>The include Action Example</title> </head> <body> <center> <h2>The include action Example</h2> <jsp:forward page="header.jsp" /> </center> </body> </html>
header.jsp
<p> Today's date: <%= (new java.util.Date()).toLocaleString()%> </p>
Now let us keep all these files in root directory and try to access index.jsp. This would display result something like as below. Here it discarded content from main page and displayed content from forwarded page only.
http://localhost:8080/index/index.jsp