The Artima Developer Community
Sponsored Link

Java Buzz Forum
Difference between page include and file include in JSP

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
Difference between page include and file include in JSP Posted: Jan 11, 2012 7:56 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: Difference between page include and file include in JSP
Feed Title: Javarevisited Blog
Feed URL: http://javarevisited.blogspot.com/feeds/posts/default?alt=rss
Feed Description: Blog on Java, Unix, Linux, FIX Protocol technology. I share my experience as tutorial, article, interview questions, programming, design etc.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Javarevisited Blog

Advertisement



This is also called difference between include directive and include action on interviews which I have seen in our last post Top 10 JSP Interview question answers , here we will see page include and file include in detail. Both include directive and include action is used to include output of a static page e.g. html or a dynamic page e.g. jsp inside another jsp file. In this article we will see what is include directive in JSP and what is include action and difference between include directive and include action. This JSP interview question is mostly asked during 2-4 years experience in Java J2EE.

This article is in continuation of my other posts on J2EE interviews like Top 10 Spring Interview Questions and Top 10 Servlet Interview questions. If you are interested on Core java or multi-threading than you can check Top 20 Core Java Interview questions.

Difference between page include and file include in JSP

 

How JSP (Java Server Pages) works

differences between page include and file include in JSP Before discussing about include directive or include action its worth remembering that how JSP pages works inside servlet container. When a request is come for JSP page, that JSP page first translated into Servlet class and than that servlet gets compiled and loaded into memory by web container. Code written using JSP declaration e.g. <%!> goes into init() method of servlet and all code from body goes into service() method of generated servlet. In case of tomcat web-server these generated servlet files goes inside work directory. In short JSP pages has three steps to get executed translation-->Compilation -->Execution.

Also JSP file will only be re-compiled only if there is a change in JSP file.

What is include directive in JSP

Include directive in JSP is used to import a static file or dynamic file e.g. JSP inside another JSP. Most common example of include directive is including header and footer in JSP. Include directive is also called file include because resource to be included is specified using file attribute as shown in below example of include directive:

<%@ include file="loan.jsp" %>

The code written in loan.jsp will be included as it is in the jsp file which included it during JSP translation time and than merged code is get compiled to generate servlet which is later used to server request. Include directive is also refereed as static import and it acts like #include from C or C++. file attribute is used specify resource to be included and can be specified in either relative url or absolute url. Since resource included using include directive is included during translation time and jsp doesn't compile if there is any change in included file. So include directive is not suitable to include dynamic resources because if included file changes between multiple request only old value is used always. include action is better choice for including dynamic resource which we will see in next section.

What is include action in JSP

Include action in JSP is another way of including a jsp file inside another jsp file. Included resource is specified using page attribute as shown in below example :

<jsp:include page="loan.jsp" %>

here contents of loan.jsp will be included during request time instead of translation time and any change in loan.jsp will reflect immediately. Due to this nature include action is also called dynamic include in JSP. It also referred as page include because of page attribute used to specify included resource.

Difference between include directive and include action.


include directive is <%@ include file="loan.jsp" %> and include action in JSP is <jsp:include page="loan.jsp" %>
This is one of popular servlet jsp interview question and mostly asked to junior or mid senior Java programmers during interviews. here is some differences between include action and include directive in jsp:

1. resource included by include directive is loaded during jsp translation time while resource included by
include action is loaded during request time.
2. Any change on included resource will not be visible in case of include directive until jsp file compiles
again. while in case of include again any change in included resource will be visible in next request.
3. include directive is static import while include action is dynamic import
4. include directive uses file attribute to specify resource to be included while include action use page attribute for same purpose.

That’s all on this popular jsp interview difference between JSP include action and include directive. Now we know what does include directive means and when to use include action or include directive. Here are some more interview questions you may like.



Read: Difference between page include and file include in JSP

Topic: First steps into Scala Previous Topic   Next Topic Topic: Lucky People

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use