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.
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.
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?
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 ]
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?
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.
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
Joined: Nov 04, 2005
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.
Joined: Sep 02, 2005
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.