Open In App

JSP – Forward action tag

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

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>
<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:

<%@ 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.

Article Tags :