Solved

JSP pageContext.include, jsp:include

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

691 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