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
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

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.

Enjoyed this post? Try these related posts

  1. JSP include action with parameter example
  2. Difference between include directive and include tag in JSP
  3. JSP forward action tag – JSP Tutorial
  4. How to validate and invalidate session in JSP
  5. Exception Implicit Object in JSP with examples
  6. Solution – No Apache Tomcat Adapter option in Eclipse IDE

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 – 2021 BeginnersBook . Privacy Policy . Sitemap