Forward Action Tag in JSP

Introduction

 
In this article we will discuss one of the action tags, “forward action”, used in JSP with a suitable example.
 

Forward action tag

 
The forward action tag forwards requests to another resource and it may be a JSP page, an HTML page or another type of resource. Tags are used to do a specific task. Actually action tags control the flow among the pages.
 
There are the following two types of forwarding action tags:
  • Without parameter
  • With parameter
forward action tag
 
Syntax of forward action tag (without parameter):
 
<jsp:forward page="relative url | <%=expression%>"/>
 
Example
 
In this example, we are just forwarding the request to the show.jsp page and retrieving the information related to the page.
 
Index.jsp
  1. <html>  
  2.    <head>  
  3.       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4.       <title>JSP forward action tag</title>  
  5.    </head>  
  6.    <body>  
  7.       <h1>This is index page</h1>  
  8.       <jsp:forward page="show.jsp"/>  
  9.    </body>  
  10. </html> 
Show.jsp
  1. <html>  
  2.    <head>  
  3.       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4.       <title>JSP forward action tag</title>  
  5.    </head>  
  6.    <body>  
  7.       <h1>Hello World!</h1>  
  8.       <p>This is what you suppose to see</p>  
  9.       <% out.print("Today's date & time:"+java.util.Calendar.getInstance().getTime());%>    
  10.    </body>  
  11. </html> 
Output
 
Output
 
Syntax of forward action tag (with parameter)
  1. <jsp:forward page="relative url | <%=expression%>">    
  2. <jsp:param name="parametername" value="parametervalue | <%=expression%>"/>    
  3. </jsp:forward>   
Example
 
In this example, we are forwarding the request to the show.jsp page with parameters and the show.jsp page prints the parameter values.
 
Index.jsp
  1. <html>  
  2.    <head>  
  3.       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4.       <title>JSP forward action tag</title>  
  5.    </head>  
  6.    <body>  
  7.       <h1>This is index page</h1>  
  8.       <jsp:forward page="show.jsp">  
  9.          <jsp:param name="name" value="Gopi Chand"/>  
  10.          <jsp:param name="site" value="c-sharpcorner.com"/>  
  11.          <jsp:param name="Date&Time" value="c-sharpcorner.com"/>  
  12.       </jsp:forward>  
  13.    </body>  
  14. </html> 
Show.jsp
  1. <html>  
  2.    <head>  
  3.       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4.       <title>JSP forward action tag</title>  
  5.    </head>  
  6.    <body>  
  7.       <h1>Hello World!</h1>  
  8.       <p>Now see the changes</p>  
  9.       My name:<%=request.getParameter("name")%><br>    
  10.       Source page:<%=request.getParameter("site")%><br>    
  11.       <% out.print("Today's date & time:"+java.util.Calendar.getInstance().getTime());%>    
  12.       Date & Time:<%=request.getParameter("Date&Time")%>    
  13.    </body>  
  14. </html> 
Output
 
prints the parameter values