JSP Forward Tag Example

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:
<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.

Let us reuse following two files
(a) header.jsp and
(b) index.jsp
Following is the content of index.jsp file:
<title>The include Action Example</title>
<h2>The include action Example</h2>
<jsp:forward page="header.jsp" />
   Today's date: <%= (new java.util.Date()).toLocaleString()%>
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.


<<Previous <<   || Index ||   >>Next >>

No comments:

Post a Comment