beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

JSP include action tag – JSP Tutorial

By Chaitanya Singh | Filed Under: JSP tutorial

Include action tag is used for including another resource to the current JSP page. The included resource can be a static page in HTML, JSP page or Servlet. We can also pass parameters and their values to the resource which we are including. Below I have shared two examples of <jsp:include>, one which includes a page without passing any parameters and in second example we are passing few parameters to the page which is being included.

Syntax: 

1) Include along with parameters.

<jsp:include page="Relative_URL_Of_Page"> 
<jsp:param ... /> 
<jsp:param ... /> 
<jsp:param ... /> 
...
<jsp:param ... /> 
</jsp:include>

2) Include of another resource without sharing parameters.

<jsp:include page="Relative_URL_of_Page" />

Relative_URL_of_Page would be the page name if the page resides in the same directory where the current JSP resides.

Example 1: <jsp:include> without parameters

In this example we will use <jsp:include> action tag without parameters. As a result the page will be included in the current JSP page as it is:

index.jsp

<html> 
<head>
<title>JSP Include example</title>
</head>
<body> 
<b>index.jsp Page</b><br>
<jsp:include page="Page2.jsp" /> 
</body> 
</html>

Page2.jsp

<b>Page2.jsp</b><br>
<i> This is the content of Page2.jsp page</i>

Output:

Content of Page2.jsp has been appended in the index.jsp.

IncludeAction-output

Example 2: Use of <jsp:include> along with <jsp:param>

index.jsp

I’m using <jsp:include> action here along with <jsp:param> for passing parameters to the page which we are going to include.

<html> 
<head>
<title>JSP Include example with parameters</title>
</head>
<body> 
<h2>This is index.jsp Page</h2>
<jsp:include page="display.jsp"> 
<jsp:param name="userid" value="Chaitanya" /> 
<jsp:param name="password" value="Chaitanya" /> 
<jsp:param name="name" value="Chaitanya Pratap Singh" /> 
<jsp:param name="age" value="27" /> 
</jsp:include> 
</body> 
</html>

display.jsp

<html>
<head>
<title>Display Page</title>
</head>
<body>
<h2>Hello this is a display.jsp Page</h2>
UserID: <%=request.getParameter("userid") %><br>
Password is: <%=request.getParameter("password") %><br>
User Name: <%=request.getParameter("name") %><br>
Age: <%=request.getParameter("age") %>
</body>
</html>

Output:

As you can see that the content of display.jsp has been included in index.jsp. Also, the parameters we have passed are displaying correctly in the included page.

include-action-with-parameters-output

Let us know if you have any doubts and questions regarding the topic. We will be happy to assist you!!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

JSP Tutorial

  • Basics of JSP
  • Life cycle of JSP
  • JSP in Eclipse IDE
  • Declaration tag
  • Expression tag
  • Scriptlets
  • Directives
  • Include Directive
  • Param Directive
  • Exception handling
  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty
  • Implicit Objects
  • Session implicit object
  • Validate session
  • Request implicit object
  • Response implicit object
  • Out implicit object
  • Application implicit object
  • Config implicit object
  • pageContext implicit object
  • Exception implicit object
  • Expression language
  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap