In this tutorial you will learn how to use the forward action in JSP file.
JSP forward Action
In this tutorial you will learn how to use the forward action in JSP file.
forward action in JSP file is used to forward / transfer a client request or processing to the another web component such as the HTML file, JSP file, a java Servlet etc in the same application context. In source file from where the forward is called rest of the code / line will not be executed.
Syntax :
<jsp:forward page={"relativeURL" | "<%= expression %>"} /> or <jsp:forward page={"relativeURL" | "<%= expression %>"} > <jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />+ </jsp:forward>
You can use either one or both as per your requirement. Here in this example I am using the second approach.
Example :
forward.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>JSP forward Example</title> </head> <body> <jsp:forward page="Forwarded.jsp"> <jsp:param name="userName" value="Devmanuals"/> </jsp:forward> </body> </html>
When you will execute the forward.jsp file it will transfer the control to the Forwarded.jsp
Forwarded.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Forwarded JSP</title> </head> <body> <% String name= request.getParameter("userName"); %> <% if(name != null) { %> <%= name %> <%} %> </body> </html>
Output :
When you will execute the above example you will get the output as :
[ 0 ] Comments