How to use Action in JSP?


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.

Following is the syntax of the forward action −

<jsp:forward page = "Relative URL" />

Following table lists out the required attributes associated with the forward action −

Sr.No.Attribute & Description
1page
Should consist of a relative URL of another resource such as a static page, another JSP page, or a Java Servlet.

Example

Let us reuse the following two files (a) date.jsp and (b) main.jsp as follows −

Following is the content of the date.jsp file −

<p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>

Following is the content of the main.jsp file −

<html>
   <head>
      <title>The include Action Example</title>
   </head>
   <body>
      <center>
         <h2>The include action Example</h2>
         <jsp:forward page = "date.jsp" />
      </center>
   </body>
</html>

Let us now keep all these files in the root directory and try to access main.jsp. This would display result something like as below.

Here it discarded the content from the main page and displayed the content from forwarded page only.

Today's date: 12-Sep-2010 14:54:22

Updated on: 30-Jul-2019

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements