JSP:INCLUDE Action Tag in Java

Introduction

This article explains the jsp:include action tag in Java. The NetBeans IDE is used for the sample example.

jsp:include Action Tag

This tag is used to include resources of other pages; it may be JSP, HTML, or another resource. This is a very important tag because it provides code reusability and saves programmer's time and reduces extra coding.

Syntax

1. Without Parameter

<jsp:include page="relativeURL | <%= some-expression %>" />

2. With Parameter

<jsp:include page="relativeURL | <%= some-expression %>">
<jsp:param name="nameOfParameter" value="valueOfParameter" | <%=expression%>"/>
</jsp:include>

Advantages

There are many advantages of this tag, some are listed below.

  • Reduce coding
  • Save time and reduce extra overhead
  • Provide code reusability

Example: 1

We use two examples. In the first example: we create two JSP pages named "index.jsp" and "ShowDate.jsp". In the index.jsp page we include another JSP page without using an attribute property to print date and time on the browser.

Use the following procedure to create this example in the NetBeans IDE.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java web" -> "Web application" as in the following.

Java Web Application

Step 3

Provide the project name as "JSPIncludeTagDemo" as in the following.

Fig-3.jpg

Step 4

Select the Java version and server wizard as in the following.

version and server wizard

Step 5

Now replace the code of "index.jsp" with the following code.

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Include Tag Demo</title>

    </head>

    <body>

        <h2>This is a index page</h2>

        <jsp:include page="ShowDate.jsp" />

    </body>

</html>

Step 6

Now create another JSP file named "ShowDate.jsp" with the following code.

ShowDate.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Page</title>

    </head>

    <body>

        <h3>The date and time of today is shown below</h3>

        <% out.print("Today is " + new java.util.Date()); %>

    </body>

</html>

Step 7

Now your project is ready to run.

Right-click on index.jsp page, then select "Run". The following output will be generated.

Output

Example: 2

Now in this example we are doing the same but we add one extra parameter, as describe below.

Step 8

Now change the code of "index.jsp" and "ShowDate.jsp" with following.

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Include Tag Demo</title>

    </head>

    <body>

        <h2>This is a index page</h2>

        <jsp:include page="ShowDate.jsp">

            <jsp:param name="pname" value="c-sharpcorner.com" />

        </jsp:include>

    </body>

</html>

ShowDate.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Page</title>

    </head>

    <body>

        <h3>The date and time of today is shown below</h3>

        <% out.println("Today is " + new java.util.Date()); %><br/>

        <b><% out.println(request.getParameter("pname")); %></b>

    </body>

</html>

Step 9

Again run your "index.jsp" file. The following output is shown there.

Output with paramters


Similar Articles