by Dinesh Thakur Category: JSP Elements

Standard syntax

<j sp: forward page= "desturl" />

This tag is used to forward the request from one JSP program to another JSP program, which participate in the chaining. This tag internally uses rd.forward(-,-) method. The task of this tag is similar to that of the forward mode of the servlet chaining. By using this tag, it can forward the request from source JSP program to the destination web resource program. But this tag discards the HTML output of the source JSP program and displays the HTML output of the destination JSP program.

 “input.htm”

<body bgcolor=”cyan" >

<center>

<form action="Square.jsp">

ENTER NUMBER <input type="text" name="t1" />

<br>

<input type="submit" value="Find" />

<input type="reset" value="Reset" />

</form>

     JSP:forward tag

input.html takes the request from user and then forwards it to Square.jsp page.

Square.jsp”        

   <center>

   <font color="red" size="5">

   SQUARE OF <%=request.getParameter("t1") %> IS:

   <%

   String num=request.getParameter("t1") ;

   int n=Integer.parseInt(num);

   if(n<50)

   {

        out.println(n*n);

    }

       else

       {

   %>

      <jsp:forward page="Cube.jsp">

      <jsp:param name="num" value="<%=n %>" />

      </jsp:forward>

      <%

        }

   %>

     JSP:forward tag

After getting the given number from the input.htrnl page, Square.jsp, calculates the
square of the program.

When an user enters a number more than 50, then the else block of Square.jsp gets
executed, where the following logic is written:

<jsp:forward page="Cube.jsp" >
<jsp:param name::;"num" value::"<%:=n %>" />
</jsp:forward>
for forwarding the request to Cube.jsp page. Then Cube.jsp code gets executed.

 “Cube.jsp”

<center>

 <font color="red” size="5">

  CUBE OF <%=request.getParameter("num") %> IS:

  <%

      String num=request.getParameter("num");

      int n=Integer.parseInt(num);

      int z=n*n*n;

      out.println(z);

  %>

  </font>

 </center>

        JSP:forward tag





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.