my dog learned polymorphism
The moose likes Servlets and the fly likes JSP include action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP include action" Watch "JSP include action" New topic

JSP include action

Chris Salivar
Ranch Hand

Joined: May 25, 2001
Posts: 34
The JSP include action goes something like:
<jsp:include page="test.jsp"/>
while the JSP include directive goes something like:
<%@include file="test.jsp"%>
Whats the significance of the fact that the JSP include action is processed at request time while the JSP include directive at translation time?
- Chris
Tony Alicea

Joined: Jan 30, 2000
Posts: 3226
High. The significance is high.
Do you know C? If so, are you familiar with the #include directive?
The include directive in JSPs directs the compiler that's going to compile a JSP, to REPLACE every include directive that it finds with the text contents of the included file. Just like #include in C.
And that's all there is to the include directive. Nothing new there if you know C.
Now to where the (include) action is!
For this you must understand that JSPs are converted literally into source code for servlets first by the JSP engine and then compiled to bytecode just like any other servlet that you may have written by hand.
The file included with the include action is converted to a servlet and compiled into bytecode (if it hadn't been already) and its output (whatever it is) is inserted at the point of inclusion in the enclosing servlet-that-corresponds-to-the-original JSP.
In other words, it's like chaining different servlets together: The first servlet (actually the compiled mother JSP) starts executing, then the servlet engine executes the one that has been included by the include action, then it goes back to the original one and so on.
I know this has not been the best explanation, but "that's what the ship brought for now..."
PS: There are consequences (differences) as to the scope of variables with each type of includes.

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Chris Salivar
Ranch Hand

Joined: May 25, 2001
Posts: 34
Cool, thanks a lot.
Can you elaborate on the consequences (differences) as to the scope of variables with each type of includes?
I agree. Here's the link:
subject: JSP include action
It's not a secret anymore!