This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why is the include JSP Standard Action like SSI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why is the include JSP Standard Action like SSI?" Watch "Why is the include JSP Standard Action like SSI?" New topic
Author

Why is the include JSP Standard Action like SSI?

Timothy Stone
Ranch Hand

Joined: Aug 01, 2001
Posts: 71
Regarding HFS&JSP 2nd Ed. page 426, question 12.

I have looked at some of the answers from c. 2007, but nothing directly asking the question, Why is Answer C more correct than Answer A.

Sure the comments on the answer provide some context to the author's thinking, but I have used "SSI" in Apache and Tomcat, and the include standard action and the directive provide SSI-like functionality as I have used SSI in the past. Is it the "dynamic content" phrasing that makes C more correct than A?

The question is provided below for context.

Thanks!
Tim

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:forward page=”/segments/footer.jspf” />
C. <jsp:include page=”/segments/footer.jspf” />
D. RequestDispatcher dispatcher = request.getRequestDispatcher(“/segments/footer.jspf”); dispatcher.include(request,response);


Timothy Stone, MIT, SCJP
http://www.petmystone.com/
"This Satan's drink [coffee] is so delicious, we shall cheat Satan and baptize it." --Pope Clement the VIII (1592-1605)
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Timothy Stone wrote:Regarding HFS&JSP 2nd Ed. page 426, question 12.

I have looked at some of the answers from c. 2007, but nothing directly asking the question, Why is Answer C more correct than Answer A.

Sure the comments on the answer provide some context to the author's thinking, but I have used "SSI" in Apache and Tomcat, and the include standard action and the directive provide SSI-like functionality as I have used SSI in the past. Is it the "dynamic content" phrasing that makes C more correct than A?

The question is provided below for context.

Thanks!
Tim

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:forward page=”/segments/footer.jspf” />
C. <jsp:include page=”/segments/footer.jspf” />
D. RequestDispatcher dispatcher = request.getRequestDispatcher(“/segments/footer.jspf”); dispatcher.include(request,response);


The question is asking
How would you include dynamic content in a JSP, similar to a
server-side include (SSI)? (Choose all that apply.)
Since the include directive (which is static) happens at the JSP translation time and the include standard action (which is dynamic) occurs at runtime, standard action is the better / correct answer in this case.


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
A. <%@ include file=”/segments/footer.jspf” %>
B. <jsp:forward page=”/segments/footer.jspf” />
C. <jsp:include page=”/segments/footer.jspf” />
D. RequestDispatcher dispatcher = request.getRequestDispatcher(“/segments/footer.jspf”); dispatcher.include(request,response);



Hi.

I just read on Wikipedia about SSI (never used it), SSI is about the text, and there it says
http://en.wikipedia.org/wiki/Server_Side_Includes

DIRECTIVE:
include

PARAMETER:
file, direct or virtual

DESCRIPTION:
This is probably the most used SSI directive, allowing the content of one document to be included in another. The file or virtual parameters specify the file (HTML page, text file, script, etc) to be included. The file parameter defines the included file as relative to the document path; the virtual parameter defines the included file as relative to the document root.


Here, important part is: THE CONTENT OF ONE DOCUMENT

There is also AN OUTPUT OF ONE (document?, file?)... JSP..... (let's say)

Small confusment is "THE CONTENT OD ONE DOCUMENT", which to an eye of an naive observer IS THE OUTPUT OF ... JSP.... Like Word document, content is that text, but actually content IS the CODE... (let's say code). That's just the small confusment...

So, We have 2:

- <% include .....
- <jsp:include ......


1. DIRECTIVE
2. STANDARD ACTION


Let's say that the name of the file being "included" is: "Header.jsp". So, the 'code' of that file is:



Nevermind my personality.

1. -including- that file with directive:



2. -including- that file with standard action



- attributes are different ( file and page )
(-and there shouldn't be html&body tags in file that is being included, more or less)

NOW:
The poant is that JSP will be translated, or compiled, or something like that, I'm not enough sure, TO THE SERVLET.

-my small nofusment in this moment is: "What if the file being included is HTML? How can it be translated into a servlet? Maybe it just cant."

So, anyway:

GENERATED SERVLET CODE FOR THE (header file) file being -included- :




1... GENERATED SERVLET CODE FOR THE JSP USING
INCLUDE DIRECTIVE




2.GENERATED SERVLET CODE FOR THE JSP USING
<JSP:INCLUDE> STANDARD ACTION




The include directive inserts the SOURCE of "Header.jsp" at translation time. But the <jsp:include/> standard action inserts the RESPONSE of "Header.jsp", at runtime

Here, actualy, the key points are also request and response objects, but also, in "this story" there is important how to pass some parameter to the 'file' (?) being included, becaouse it is needed not only to 'include' but to customize the 'includee'. This thing is observable from the question. The question is nice. Forward page and request dispatcher "don't include". The question is nice becaouse of all of things that it shows, req&resp objects "travelling", output composing, etc. , and even the use of attributes and parameters not visible in the question.

What else?

-include directive at first request does the work, and from the second there is no extra runtime overhead
-include standard action has less work at translation time, and more work with -each- request (especially if the included file is JSP)
- Can any of two of them change the headers, or set cookies? (I'm not sure what I read, but I think I read NO)

-if the file being included changes, most servers will retranslate, but it is not guaranteed.

-customization is done with <jsp:param> (and probablly attributes in request can "travell" )

- <jsp:forward> flushes the buffer, and then forwards ( flush-and-forward) (..and I think that I understood that in that case an exception is thrown "visible on the old page" that was flushed)... so , if anything exsisted on the PAGE before the 'forward' it was cleared (BUFFER)

I'm accedentally 'totally into this', learning it at the moment, now.

Bye.Bye
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
...Aha....D has dispatcher.include
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
Why C is more than D ???

is the rightier question.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1662
    
  25

Hi Timothy, Rajeev and Petar,

The original question:
Is it the "dynamic content" phrasing that makes C more correct than A?

Yes, that is correct. Both are forms to include a file: one done in the translation phase and the other at request time.

According to the specifications JSP.1.10.3 The include Directive
A JSP container can include a mechanism for being notified if an included file changes, so the container can recompile the JSP page. However, the JSP 2.0 specification does not have a way of directing the JSP container that included files have changed.

So it should actually be used for static content lik copyright symbols, page header and so on: things that hardly ever change as you are not sure whether the container will recompile the page.

and a little bit further on JSP.1.10.5 Including Data in JSP Pages
An include directive regards a resource like a JSP page as a static object; i.e. the text in the JSP page is included. An include action regards a resource like a JSP page as a dynamic object; i.e. the request is sent to that object and the result of processing it is included.

Regards,
Frits
Icaro Dourado
Greenhorn

Joined: Dec 30, 2007
Posts: 9
This question is not fair!

Despite the fact that we should avoid using include directive for things we know that may change in the future, it can STILL be used for dynamic content due the fact we can include JSP code (which provides dynamic behavior), for example. That makes alternative A correct, in my opinion.

Plus, alternative D is also correct! It is similar to alternative C, the question does not say we could not use scriptlets in our JSPs.

By pointing out these arguments, A B and D seem all correct.


SCJP 5, SCWCD 5
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

D is not correct, as this is a pure Java code and not a scriptlet. It lacks the <% ... %> construct.

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

<jsp:include> is correct one since this includes the dynamic content to the response output stream buffer...

This construct generates dynamic content based on the request it receives...

and this action is performed at the run time...

and

<%@include %> directive can be used to include the static content... and the inclusion of code is done "at" compile time itself...

Its something similar to "Preprocessor directives" in C... which is done before compile time...

If there is a common code which has to be used in multiple JSPs... Instead of writing the common code in all JSPs repeatedly...

It can be put in one JSP and can be included in multiple JSPs with one line statement <%@include %>


SCJP 6 [SCJP - Old is Gold]
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Well, as the D option is 100% incorrect because of the reason I pointed above, the A can be not so obvious in my opinion.

It is true, that the container when sees the include directive, is including this file (not page) during translation time, so it is used mainly for static content which is not a subject of dynamic changes.
But it doesn't mean you cannot add a dynamic content in the included file. As an example:

test3.jsp



test.jsp



The result for request:
http://localhost:8080/myApp/test.jsp?test=Howdy%20Rachners

is
Including static file: The Request parameter 'test' is equal to: Howdy Rachners. End of static included file. After inclusion.


So, it's different in case different test parameter is set. The inclusion is static, but it may introduce some dynamic features.

The point is, that you cannot change the test3.jsp file without recompilation of test.jsp. This is what dynamic is referring to in the cited question.

Cheers!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why is the include JSP Standard Action like SSI?