Saturday, June 15, 2013

jsp:include- An Action element to include content of other pages

Hi All,
Welcome to Java-recent,a blog explaining Java related concepts.

Today we will discuss about jsp:include - an Action element.
Agenda:-
  • jsp:include
  • Differences between jsp:include and Include directive element

Action elements change the runtime behavior of a JSP page
some Action elements are :-
  • jsp:forward
  • jsp:include
  • jsp:useBean
  • jsp:setProperty
  • jsp:getProperty

    jsp:include :- This acts like include directive, but has some differences.
  • jsp:include is used to insert other jsp page content into current page.
  • Its nothing but like a method call during run time.
  • The inclusion of the page happens at run time,so if the included file is modified the change will gets reflected
  • request and response objects are shared
  • jsp:include is good for placing dynamic content like a list retrieved from data base etc.

Syntax: - <jsp:include age=”relative URL / >

Example :- We have three jsp pages
  • Header.jsp :- is used to print current date and time-- dynamic content
  • Presentation.jsp :- is the page where Header.jsp and Footer.jsp are included
  • Footer.jsp :- copy right information—static content
    All the three JSP pages are placed in same directory

Header.jsp
<%@page import="java.util.Date" %>
<%
Date date =new Date();
%>
<%=date %>

Footer.jsp
<body>
<html>
<h4>Copyright © Javarecent.com 2013</h4>
</body>
</html>

Presentation.jsp
<center>
<h4>Hi,you visited the page@ </h4>
<jsp:include page="Header.jsp" />
<h3>Welcome to Java-recent.blogspot.com</h3>
<jsp:include page="Footer.jsp" />
</center>


Run/access the presentation.jsp page in the browser after deploying in a web server,you will get a similar output

Output :-

Hi,you visited the page@
Sat Jun 15 17:39:05 IST 2013  -->  content from Header.jsp[dynamic content]

Welcome to Java-recent.blogspot.com

Copyright © Javarecent.com 2013   --------   content from Footer.jsp[static content]


Here in presentation.jsp we have included Header.jsp and Footer.jsp


Differences between Include directive and jsp:include

Include directive
Jsp:include
It’s a Directive element It’s a Action element
Syntax <% @include file=”header.jsp %> Syntax < jsp:include age=”currentTime.jsp / >
Included during translation phase from JSP page to Java class Included during run time/when request has spawned
Suitable for static content/data Suitable for dynamic content/data




Happy Learning

Please provide your valuable comments on this article and share it across your network.


No comments:

Post a Comment

Like and Share