Open In App

JSP – Include Action Tag

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

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

  • First, we will create multiple JSP pages including a page with generic content where we want to add generic content.
  • Then, we will use the <jsp:include> tag to specify which JSP page to include in the JSP page.
  • After that, it will handle the request and generate dynamic content in the included JSP pages.
  • Finally, we combine the contents of the display as a final output to include the page and the page.

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:

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

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

HTML
<!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

  • When any end user goes to the index.jsp page, he will see a message i.e. “Welcome to Include Example” message.
  • The <jsp:include> tag in index.jsp page includes the content of header.jsp as well as the footer.jsp at the appropriate locations.
  • Then the final message will be displayed.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads