Solved

JSP pageContext.include, jsp:include

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
ArrayLists in ArrayLists/HashMaps? 4 40
Checkbox and ListView in Android Layout 4 38
recursion example 16 68
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 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