aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes include action vs. include directive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "include action vs. include directive" Watch "include action vs. include directive" New topic
Author

include action vs. include directive

Marcos Urata
Greenhorn

Joined: Dec 31, 2004
Posts: 26
Hello,

I�ve been looking through the jsp spec. and I just couldn�t understand the example given on section 5.4.

"...Given the following jsp file: A.jsp, C.jsp, dir/B.jsp and dir/C.jsp.

* A.jsp says <jsp:include page="dir/B.jsp"/> and dir/B.jsp says <%@include file="C.jsp" %>. In this case the relative specification C.jsp resolves to dir/C.jsp.

* A.jsp says <%@include file="dir/B.jsp" %> and dir/B.jsp says <jsp:include page="C.jsp" />. In this case the relative specification C.jsp resolves to C.jsp." (WHY ???)

I don�t understand why the second one would resolve to C.jsp instead of dir/C.jsp.

The spec. on page 45 also seems to show no difference at all between the relative path used in the include action and the include directive. Both are relative to the JSP page!!

"For an include directive (see Section JSP.1.10.3) where the path is used in a file attribute, the interpretation is relative to the JSP file or tag file. For a jsp:include action (see Section JSP.5.4) where the path is used in a page attribute, the interpretation is relative to the JSP page."
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
Have you tried to make some pages like A, B and C and run it in your servlet container?


SCJP 1.4 SCWCD 1.4
Marcos Urata
Greenhorn

Joined: Dec 31, 2004
Posts: 26
oh yes!! And it those examples are right!!
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Sorry, but they are not the same. Read again what I "quote" from your own post. One is relative to the JSP file and the other relative to the JSP page.

Originally posted by Marcos Urata:
Both are relative to the JSP page!!

"For an include directive ... relative to the JSP file or tag file.
For a jsp:include action ... relative to the JSP page."
Marcos Urata
Greenhorn

Joined: Dec 31, 2004
Posts: 26
ok Jose, thanks for your answer, but what I don�t understand is how the 4th example resulted in C.jsp instead of dir/C.jsp.

As I understand, the 2nd one should resolve to C.jsp.

Thanks
Marcos
Marcos Urata
Greenhorn

Joined: Dec 31, 2004
Posts: 26
Sorry, I forgot to post the 4 examples from the spec.


"For an example of a more complex set of inclusions, consider the following
four situations built using four JSP files: A.jsp, C.jsp, dir/B.jsp and dir/C.jsp:

A.jsp says <%@ include file=�dir/B.jsp�%> and dir/B.jsp says <%@ include
file=�C.jsp�%>. In this case the relative specification C.jsp resolves to dir/C.jsp.
� A.jsp says <jsp:include page=�dir/B.jsp�/> and dir/B.jsp says <jsp:include
page=�C.jsp� />. In this case the relative specification C.jsp resolves to dir/C.jsp.
� A.jsp says <jsp:include page=�dir/B.jsp�/> and dir/B.jsp says <%@ include
file=�C.jsp� %>. In this case the relative specification C.jsp resolves to dir/C.jsp.
� A.jsp says <%@ include file=�dir/B.jsp�%> and dir/B.jsp says <jsp:include
page=�C.jsp�/>. In this case the relative specification C.jsp resolves to C.jsp."
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
the include directive is processed at translation time, so where ever it appears, it's path is always relative to the file, where it was declared!

where as include jsp action always takes path relative to the page translation unit as it's executed at request time.

hope this clears the confusion....
Marcos Urata
Greenhorn

Joined: Dec 31, 2004
Posts: 26
Thank you praven!!! Now things make sense!
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
I'm still confused... I still do not see why it resolves to C.jsp.


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
You have to know that:
1) "For a jsp:include action where the path is used in a page attribute, the interpretation is relative to the JSP page."

2) Only the include jsp action (and not the include directive, which works at translation time) changes the CURRENT PAGE.

So when file A.jsp says
<%@ include file=�dir/B.jsp�%>
the current page doesn't change (it will remain as A.jsp).

Then, when file dir/B.jsp says <jsp:include page=�C.jsp�/>, the current page is A.jsp and so, this action resolves to C.jsp.

Is it clear now?
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
I must admit that the explanation is more challenging than the question itself! Let me also try....

When A.jsp includes B.jsp either by include directive stmt or include action page stmt, A.jsp expects B.jsp to be present in the current directory. (always w.r.t to the parent directory in which A.jsp resides. There is no confusion here. Right?)

The confusion begins to occurs only when A.jsp includes B.jsp and B.jsp includes C.jsp

In the above case, A.jsp can find B.jsp w.r.t to its current directory.
But when it comes to finding C.jsp, B.jsp follows the foll. algorithm to resolve the path to C.jsp

1. If B.jsp itself is included by include directive stmt by a parent jsp (A.jsp) then C.jsp would be in the directory where A.jsp is present.

2. If B.jsp itself is included by include action page stmt by a parent jsp (A.jsp) then C.jsp would be in the directory where B.jsp is present.

It might help to imagine include directive stmt as like a macro as in (C language). All the contents of the included JSP are merged in the including(parent) JSP at compile time(a.k.a page translation time) itself.
So there is no change in the directory to locate the include file.

While in the case of include action page stmt, All the contents of the included JSP are merged in the including(parent) JSP at run time.
The include action page stmt could be imagined as if the JSP Engine is trying to change its current directory to locate the included
jsp page! Further include stmts inside the included jsp page will be w.r.t to the directory in which the included file resides!

Apologies...if I have added more confusion to this thread!.....
thanks Marcos! I think this is a good question and it also cleared up my understanding!
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
I tried rereading it so many times.. and I"m still lost... I tried to look at the example


A.jsp says <%@ include file=�dir/B.jsp�%> and dir/B.jsp says <%@ include
file=�C.jsp�%>. In this case the relative specification C.jsp resolves to dir/C.jsp.
� A.jsp says <jsp:include page=�dir/B.jsp�/> and dir/B.jsp says <jsp:include
page=�C.jsp� />. In this case the relative specification C.jsp resolves to dir/C.jsp.
� A.jsp says <jsp:include page=�dir/B.jsp�/> and dir/B.jsp says <%@ include
file=�C.jsp� %>. In this case the relative specification C.jsp resolves to dir/C.jsp.
� A.jsp says <%@ include file=�dir/B.jsp�%> and dir/B.jsp says <jsp:include
page=�C.jsp�/>. In this case the relative specification C.jsp resolves to C.jsp."


To understand but no luck
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
Sorry Francis,
But why don't you try a sample with the above mentioned scenarios in Tomcat 5.0 and check it out. It might give you some idea.
[ March 24, 2005: Message edited by: Vishwa Kumba ]
 
 
subject: include action vs. include directive