Open In App

JSP – Forward action tag

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaServer Pages in Java can provide a way to create dynamic web pages. Forward action tag is a tool in JSP that is used to forward control from one JSP page to another JSP by the same application. It allows developers to create modular and reusable components to build web applications.

Forward action tag in the JSP

Forward action tag in JSP allows the client to forward a request from one JSP page to another without the browser. This approach is useful for modularizing web applications and split the tasks across multiple JSP pages. When the request is submitted, the URL in the browser does not change and the client is unaware of the server-side operation.

Step-by-Step Implementation

  • Create JSP pages: We can create multiple JSP pages representing different parts or concepts of a web application.
  • Use Forward Action Tag: To refer to the target JSP page we can use the <jsp:forward> tag to which the control should be forwarded.
  • Modify the request on the target page: We can execute the request sent to the target JSP page and then retrieve the request parameters or perform the necessary server-side operations.
  • Display Output: Finally, create dynamic objects or display information based on submitted requests.

Project to Implement JSP Forward action tag

Below we have created index.jsp and Tareget.jsp and tested the output in the browser.

index.jsp:

HTML
<html>
<head>
    <title>Forward Example</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            padding: 20px;
        }
        h2 {
            color: #333;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <h2>Welcome to the Forward Example</h2>
    <p>This is the index page.</p>
    <jsp:forward page="target.jsp"/>
</body>
</html>

Target.jsp:

HTML
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Target Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f9f9f9;
            padding: 20px;
            text-align: center;
        }
        h2 {
            color: #333;
        }
        p {
            color: #666;
        }
    </style>
</head>
<body>
    <h2>Target Page</h2>
    <p>This is the target page.</p>
</body>
</html>

Output:

Below we can see the output in browser.

Output Screen


When the users reach the index.jsp page, they will see the message “Target Page” with the paragraph “This is the target page.” The <jsp: forward> tag in the index.jsp forwards the request to the target.jsp and the user see the contents target.jsp including the message of the target page.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads