Solved

JSP pageContext.include, jsp:include

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

929 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

13 Experts available now in Live!

Get 1:1 Help Now