<jsp:forward> action element :-


jsp:forward action element is used to forward the client request to some other static or dynamic web resource such as html, servlet, jsp from currently executing jsp file. It has the following syntax -

<jsp:forward page="relativeURL" />        
Where - page attribute specify the url of the web resource where you want to forward request.

We can also send some information in the form of parameters to the forwarded web resource using the following syntax -
<jsp:forward page="relativeURL" >
<jsp:param name="parameter_name" value="parameter_value />
.........
</jsp:forward>  
        
We can specify one or more parameters with jsp:forward.

Example -

This simple example will show you how can we use jsp:forward within jsp files.
1- index.jsp - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Welcome to index.jsp page</h1>
        <jsp:forward page="Home.jsp">
            <jsp:param name="username" value="rohit kamboj"></jsp:param>
        </jsp:forward>
    </body>
</html>   

2- Home.jsp - Create a Home.jsp file as follows -
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        out.println("<h2>Hello Mr "+request.getParameter("username")+"</h2>");
        out.println("<h3>Welcome Home</h3>");
        out.println("<h3>Today is - </h3>"+new java.util.Date().toString());
        %>
    </body>
</html>  

3- web.xml - Make index.jsp your default page.
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	 version="3.1">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>    

4- Now run the project, It will forward you from index.jsp (currently executing file) to Home.jsp -



Download this example (NetBeans implementation) - Click Here