Open In App

JSP – Include Action Tag

JavaServer pages in Java provide a powerful way to build web pages. The Include Action Tag is an object in JSP that can be used to insert the content of one JSP page into another JSP page during the translation phase. This approach allows developers to reuse common elements across multiple pages.

Step-by-Step Implementation

Implementation of Include action tag in JSP

Now, we will develop a simple JSP application that will demonstrate the Include Action Tag of JSP.

index.jsp:

<!DOCTYPE html>
<html>
<head>
    <title>Include Example</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f9f9f9;
            padding: 20px;
            text-align: center;
        }
        h2 {
            color: #333;
        }
        p {
            color: #666;
        }
        /* Styles for header and footer */
        header {
            background-color: #4CAF50;
            color: white;
            padding: 10px;
        }
        footer {
            background-color: #333;
            color: white;
            padding: 10px;
            position: fixed;
            bottom: 0;
            width: 100%;
        }
        nav ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }
        nav ul li {
            display: inline;
            margin-right: 10px;
        }
        nav ul li a {
            color: white;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <h2>Welcome to the Include Example</h2>
    <p>This is the index page.</p>
    <jsp:include page="header.jsp"/>
    <p>This is some content below the header.</p>
    <jsp:include page="footer.jsp"/>
</body>
</html>


header.jsp:

<!DOCTYPE html>
<html>
<head>
    <title>Header</title>
</head>
<body>
    <header>
        <h1>Header Content</h1>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </header>
</body>
</html>


footer.jsp:

<!DOCTYPE html>
<html>
<head>
    <title>Footer</title>
</head>
<body>
    <footer>
        <p>&copy; 2024 Example Company</p>
    </footer>
</body>
</html>

Output:

Below we can see the output screen in browser.

Output Screen

Article Tags :