*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Dynamic Content, include directive and std action 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 "Dynamic Content, include directive and std action" Watch "Dynamic Content, include directive and std action" New topic
Author

Dynamic Content, include directive and std action

M Jairam
Greenhorn

Joined: Nov 04, 2005
Posts: 22
The following question is from HFSJ page 422.
Question: How would you include dynamic content in a JSP, similar to a server-side include (SSI)? (Choose all that apply.)

A. <%@ include file="/segments/footer.jspf" %>
B. <jsp:include page="/segments/footer.jspf" />

The answer in the book says only Option B is correct. It says A is wrong because include directive is for static include which happens at translation time.

My questions is given A and B, I don't see anything that would be different between the two. Option B does not make use of <jsp aram .../> or the page attribute value is also not an expression, thus, what makes option B dynamic and not option A? Please let me know. Thanks.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

Option B is dynamic inclusion, because this action execute the code in the included file and give back only the response. The codes are not part of the including JSP, it is a seperate instance of included file ( in case os JSP,Servlet)which execute the code and sent back the response, in ideal cases. But the include directive include the codes in the JSP, like copy and paste at the time of compilation. To compile the including JSP successfully, there are some resctriction to be followed in included JSP.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
M Jairam
Greenhorn

Joined: Nov 04, 2005
Posts: 22
Can anybody give me an example of dynamic content that would only work with option B but not option A.
Narendra: Your response is not based on the options A and B. It is based on the general description of include directive and include standard action. The question is related to a specific case of these jsp elements. If you disagree, can you give me an example using option A and B where you would have dynamic content in one and not the other?

Thanks.
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Hi Jairam,

The best to clear your doubt, is to try it out.
The go to the /tomcat path/works/localhost/.. and check the
generated servlet_jsp file.

I go for B. because all directives are resolved at translation
time, while all standared action are resolved at runtime.
Hope it helps. My exams is on MOnday O1 .



SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
The difference between dynamic include(standard action) and static include(directive) is simple.

In static include at the page translation time(which happens only once in jsp's life) the entire code is copied and pasted into your page.

Whereas in dynamic include for every incoming request a dynamic call is made to the included page and only the response of it is pasted in your current page. To learn more and see whats actually in there you better look into the generated servlet code as adviced by Mohammed. You also look at page 401 in HFSJ.
[ December 01, 2005: Message edited by: Vishnu Prakash ]

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
M Jairam
Greenhorn

Joined: Nov 04, 2005
Posts: 22
Please note that I fully understand how include directive works and how include standard action works - that is, one includes the content of the file into the main servlet at translation time and the other creates a separate servlet and every time the request comes makes a call to this separate servlet to get the contents included in the final response. This only explains what happens on the server.

But the questions is not asking what happens on the server. It is asking for dynamic content - meaning, same request made at different times should produce possibly different result or view - as viewed on the browser (not how it is done on the server).

Now, given option A or B, how can we say one can produce dynamic content and not the other?
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
Jairam,
To give you an example how dynamic jsp:include is useful, lets assume you have an application where in home page you are displaying an image/html from your server file system. Administrator will have an option where he can go and upload different new image (or html containing image). From here onwards if users access your application they should be able to see new image.. Will this be possible using static include?? NO. If you had used <%@include..> directive that file would have translated at translation time and it never looks dynamically to include that file. To solve this,
you need to have <jsp:include page="some file"/> which includes file everytime at request time to reflect your new image changes.
I am not telling you in detail, how to implement. But when I had a problem like this in one of my application I had to solve it using <jsp:include>
Hope it is clear to U.


Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
M Jairam
Greenhorn

Joined: Nov 04, 2005
Posts: 22
Rizwan:

Any reference to an image file on a generated page is handled the same way - the browser will make a request for the second time to the image source (on the server) to retrieve the image - this happens irrespective of if the page was generated by a single jsp servlet (such as in include directive) or if the page was generated by a jsp servlet which included one or several other jsp servlet response via jsp include standard action.

In summary, if you change the image file on the server, in either case you will see the most recent image file (unless caching is turned on, then in both the cases you might see the older cached image).

However, note that I am aware of the fact that, say you have J1.jsp which includes J2.jsp, then with the directive, if you change J2.jsp, it will continue to use the content of older J2.jsp (which is now part of J1.jsp) until you change J1.jsp or delete the geneated J1's servlet class file.

Having said that, I don't think the question was realted to generating dynamic content by changing the file J2.jsp. When generaly "dynamic content" (such as jsp file) as against "static content" (html file), is being referred, "dynamic content" is generated not by changing the underlaying file on the server, but by variables such as the specific user who is making the request or other variable factors such as time of request etc.

In summary, my comment is that you can generate "dynamic content" in both cases - option A and B.
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
Jairam,
I think question should be little bit different to make it clear, it should have been used 'dynamic include' instead of 'dynamic content'. Otherwise, I think u understand the difference between dynamic include and static include. Question also says similar to 'server side include', which is similar as <jsp:include>. U cannot have 'server side include' with the include directive. Include directive is something introduced in JSP for convenience and to reuse the code. <jsp:include > is similar to what u do in servlet to include using request dispatcher.
hth.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Content, include directive and std action