BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSP forward action tag

Last Updated: July 1, 2024 by Chaitanya Singh | Filed Under: JSP tutorial

JSP forward action tag is used for forwarding a request to the another resource (It can be a JSP, static page such as html or Servlet). Request can be forwarded with or without parameter. In this tutorial we will see examples of  <jsp:forward> action tag.

Syntax: 

1) Forwarding along with parameters.

<jsp:forward page="display.jsp"> 
<jsp:param ... /> 
<jsp:param ... /> 
<jsp:param ... /> 
...
<jsp:param ... /> 
</jsp:forward>

2) Forwarding without parameters.

<jsp:forward page="Relative_URL_of_Page" />

Relative_URL_of_Page: If page is in the same directory where the main page resides then use page name itself as I did in the below examples.

Example 1: JSP Forward without passing parameters

In this example we are having two JSP pages – index.jsp and display.jsp. We have used <jsp:forward> action tag in index.jsp for forwarding the request to display.jsp. Here we are not passing any parameters while using the action tag. In the next example we will pass the parameters as well to another resource.

index.jsp

<html> 
<head>
<title>JSP forward action tag example</title>
</head>
<body> 
<p align="center">My main JSP page</p>
<jsp:forward page="display.jsp" /> 
</body> 
</html>

display.jsp

<html>
<head>
<title>Display Page</title>
</head>
<body>
Hello this is a display.jsp Page
</body>
</html>

Output:

Below is the output of above cpde. It is basically the content of display.jsp, which clearly shows that index.jsp didn’t display as it forwarded the request to the display.jsp page.

JSPForward-Output

Example 2: JSP Forward with parameters

Here we are passing the parameters along with forward request. For passing parameters we are using <jsp:param> action tag. In this example we are passing 4 parameters along with forward and later we are displaying them on the forwarded page. In order to fetch the parameters on display.jsp page we are using getParameter method of request implicit object.

index.jsp

<html> 
<head>
<title>JSP forward example with parameters</title>
</head>
<body> 
<jsp:forward page="display.jsp"> 
<jsp:param name="name" value="Chaitanya" /> 
<jsp:param name="site" value="BeginnersBook.com" /> 
<jsp:param name="tutorialname" value="jsp forward action" /> 
<jsp:param name="reqcamefrom" value="index.jsp" /> 
</jsp:forward> 
</body> 
</html>

display.jsp

<html>
<head>
<title>Display Page</title>
</head>
<body>
<h2>Hello this is a display.jsp Page</h2>
My name is: <%=request.getParameter("name") %><br>
Website: <%=request.getParameter("site") %><br>
Topic: <%=request.getParameter("tutorialname") %><br>
Forward Request came from the page: <%=request.getParameter("reqcamefrom") %>
</body>
</html>

Output:

Above code directly displayed display.jsp page, which is displaying the parameters passed from index.jsp page.

forward-param-output

Top Related Articles:

  1. JSP Scriptlet Tag (Scripting elements)
  2. Include Directive in JSP
  3. Java Server Pages (JSP) Life Cycle
  4. Difference between include directive and include tag in JSP
  5. Session Implicit Object in JSP with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Pankaj says

    March 31, 2015 at 7:05 AM

    can you explain the difference b/w include and forward action tag

    Reply
    • Nikhil says

      July 27, 2016 at 11:21 AM

      include is like circular linked list and forward is like linear linked list.

      Reply
      • xywei says

        November 16, 2016 at 3:01 AM

        yeah. your opinion is good!

        Reply
  2. Jomy Joseph says

    March 28, 2016 at 7:56 AM

    how to redirect control from html page to jsp page using form action method

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap