How do I include other pages using <jsp:include>?

The <jsp:include/> tag is use to include another page fragment of a JSP page into another page. This is useful when you have a common page such as header, footer or a menu that applied to many of all of your pages.

<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
    <title><jsp:include/> Demo</title>
<div id="header">
    <jsp:include page="common/header.jsp"/>

<div id="main">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna

<div id="footer">
    <jsp:include page="common/footer.jsp"/>

Here are the page fragment of the header.jsp, footer.jsp and menu.jsp. All of them are placed in the common folder in the same location with the index.jsp file.


<strong><jsp:include/> Demo</strong>
<jsp:include page="menu.jsp"/>


© 2019 Kode Java Org.


<a href="/index.jsp">HOME</a>

When you access your page (http://localhost:8080/include/main.jsp) from the servlet container such as Apache Tomcat you’ll have a complete display of a page that contains header, menu, content and footer.

Here is the directory structure of our example:

├── pom.xml
└── src
    └── main
        └── webapp
            ├── WEB-INF
            │   └── web.xml
            ├── include
            │   ├── common
            │   │   ├── footer.jsp
            │   │   ├── header.jsp
            │   │   └── menu.jsp
            │   └── main.jsp
            └── index.jsp

The web.xml configuration file.

<?xml version="1.0" encoding="UTF-8"?>

  <!-- Config here. -->


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.