Solved

JSP pageContext.include, jsp:include

Posted on 2011-09-06
4
706 Views
Last Modified: 2012-05-12
i would like to know about JSP pageContext.include, jsp:include. where, why, how we use them. Advantages, disadvantages, differences of them. i was not clear on this concept. thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 36493571


I think this gives a good explanation:

http://www.jguru.com/faq/view.jsp?EID=532194

There are three (main) ways to include a file in a JSP:

1. <% pageContext.include(String relativeUrlPath); %>
2. <jsp:include page="relative URL" flush="true" />
3. <%@ include file="relative url" %>

The first (#1) allows you to specify a variable url in your JSP. relativeUrlPath can be calculated. The last two only allow you to include a literal (hardcoded) String as the URL.

The last one (#3) lets you include files at the time the JSP page is translated into a servlet. They actually become part of the generated servlet code. Changing the file that is included won't have an effect until the jsp is modified and recompiled. Whereas the first two include the file at execution time so modifications to the included file will be noticed the next time the page is executed.
0
 
LVL 2

Assisted Solution

by:themuppeteer
themuppeteer earned 250 total points
ID: 36493794
I could add to this that the

<%@ include file="relative url" %>

Open in new window


copies the code from the file into the place where the @include is at the time
the jsp is compiled. The output of this process is 1 jsp that you use in the program.
Be careful with headers and scripting in the included file because it gets copied right in your resulting jsp.
Say your included file has this at the top:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

Open in new window

or some javascript,
then it will be copied in the resulting jsp again at the place where the @include occurs.
You really should only include snippets of code with the @include.
Also @include will do its thing at translation time. The result is a servlet that stays the same.

The jsp:include will work a bit different. It will execute the servlet (so at request time), and when it gets to the 'jsp:include', it will execute the included servlet and use the output of this servlet.

btw, don't use <% %> (scriptlets)..its very dirty and becomes a mess.

hope this helps
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36935266
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question