Solved

JSP pageContext.include, jsp:include

Posted on 2011-09-06
4
679 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 75
arguments to jar 5 26
Java string replace 11 43
jsp login check 12 28
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now