BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

JSP include directive with parameters example

By Chaitanya Singh | Filed Under: JSP tutorial

In the last tutorial we  discussed JSP include action with parameters. Here we will see how to pass parameters when using JSP include directive.

Example

In this example we are passing three string parameters to the included JSP page.

index.jsp

<html>
<head>
<title>Passing Parameters to Include directive</title>
</head>
<body>
<%@ include file="file1.jsp" %>
<%!
String country="India"; 
String state="UP";
String city="Agra";
%>
<% 
session.setAttribute("co", country);
session.setAttribute("st", state);
session.setAttribute("ci", city);
%>
</body>
</html>

Above, I have used declaration tag for initializing strings and scriptlet for setting up them in session object. As the use of sciptlet is disregarded a long back, alternatively you can use <c:set> JSTL tag for doing the same – The code would be like this –

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="co" value="India" scope="session"/>
<c:set var="st" value="UP" scope="session"/>
<c:set var="ci" value="Agra" scope="session"/>
<%@ include file="file1.jsp" %>

file1.jsp

<%=session.getAttribute("co") %>
<%=session.getAttribute("st") %>
<%=session.getAttribute("ci") %>

Output:

include-directive-param

In the above example I have passed the parameters using session implicit object, however you can also pass them using request, page and application implicit objects.

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

Scripting Elements

  • Scriptlet Tag
  • Expression tag
  • Declaration tag

Implicit Objects

  • Implicit Objects
  • JSP Request
  • JSP Response
  • JSP Config
  • JSP Application
  • JSP Session
  • JSP Out
  • JSP pageContext
  • JSP Exception
  • Validate session

JSP directives

  • JSP Directives
  • Include Directive

JSP Exception

  • Exception handling

JSP Action

  • Action tags
  • Include action
  • Forward action
  • useBean, setProperty & getProperty

Expression language

  • Expression language

JSP Custom Tags

  • Custom Tags
  • Custom tag example
  • JSP Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap