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
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
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP forward action tag</title>
- </head>
- <body>
- <h1>This is index page</h1>
- <jsp:forward page="show.jsp"/>
- </body>
- </html>
Show.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP forward action tag</title>
- </head>
- <body>
- <h1>Hello World!</h1>
- <p>This is what you suppose to see</p>
- <% out.print("Today's date & time:"+java.util.Calendar.getInstance().getTime());%>
- </body>
- </html>
Output
Syntax of forward action tag (with parameter)
- <jsp:forward page="relative url | <%=expression%>">
- <jsp:param name="parametername" value="parametervalue | <%=expression%>"/>
- </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
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP forward action tag</title>
- </head>
- <body>
- <h1>This is index page</h1>
- <jsp:forward page="show.jsp">
- <jsp:param name="name" value="Gopi Chand"/>
- <jsp:param name="site" value="c-sharpcorner.com"/>
- <jsp:param name="Date&Time" value="c-sharpcorner.com"/>
- </jsp:forward>
- </body>
- </html>
Show.jsp
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP forward action tag</title>
- </head>
- <body>
- <h1>Hello World!</h1>
- <p>Now see the changes</p>
- My name:<%=request.getParameter("name")%><br>
- Source page:<%=request.getParameter("site")%><br>
- <% out.print("Today's date & time:"+java.util.Calendar.getInstance().getTime());%>
- Date & Time:<%=request.getParameter("Date&Time")%>
- </body>
- </html>