Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help: inconsistent explanation on jsp:forward?

 
Kelvin Hung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
These two questions are taken from j2eecertificate.com. The explaination of jsp:forward are different between Question 1) and 2). So which one is correct?
Question 1)
Which of the following statements correctly declares an element that allows the output result of a specified resource to be inserted in a JSP page? [Check all correct answers]


1 <jsp:include file="disclaimer.html" />
2 <jsp:forward page="disclaimer.html" flush="true" />
3 <jsp:include page="disclaimer.html" flush="true" />
4 <%@ include file="disclaimer.html" %>
5 <jsp:forward page="disclaimer.html" />

Answers 3 and 5 are correct... The jsp:forward action "includes the output generated from the resource" specified by the page attribute into the including JSP. However, jsp:forward terminates the execution of the current page whereas the jsp:include action resumes processing in the calling JSP page, once the inclusion is completed.
Question 2
You have a plain text file that does not contain any JSP code. Which of the following correctly defined elements can be used to include the file in a JSP page? [Check all correct answers]


1 <%@ include file="companypolicy.txt" %>
2 <%@ include file="<%=policyfilename%>" %>
3 <jsp:include file="companypolicy.txt" />
4 <jsp:forward page="companypolicy.txt" />
5 <jsp:include page="companypolicy.txt" />

Answers 1 and 5 are correct. Answer 4 is incorrect because "jsp:forward does not include the file in a JSP page" but forwards to it instead.

Any help is appreciated.
Kelvin
 
Mehdi Chaouachi
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to use the <jsp:forward> u need to forword to dynamic resource, like a servlet ora jsp page, because the forward action will pass the request and response objects as parameters to the forworded page, which is impossible when that page is static, and it will not be able to process the request and send a response to the client.
 
Kelvin Hung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ic. thanks!
 
hover cheng
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mehdi Chaouachi:
to use the <jsp:forward> u need to forword to dynamic resource, like a servlet ora jsp page, because the forward action will pass the request and response objects as parameters to the forworded page, which is impossible when that page is static, and it will not be able to process the request and send a response to the client.

I hope your are serious. I tested two files on my Tomcat 5.0.16:
one is forward.jsp :
<%@include file="/header.jspf"%>
<jsp:forward page="tt.txt"/>
<%@include file="/footer.jspf"%>
the other is tt.txt :
This is the output from tt.txt!
And the result of http://localhost:8080/jsptest/forward.jsp is:
This is the output from tt.txt!
 
hover cheng
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kelvin,
I do think the mock test you are taking is in a mess.

Question 1)
Which of the following statements correctly declares an element that allows the output result of a specified resource to be inserted in a JSP page? [Check all correct answers]

1 <jsp:include file="disclaimer.html" />
2 <jsp:forward page="disclaimer.html" flush="true" />
3 <jsp:include page="disclaimer.html" flush="true" />
4 <%@ include file="disclaimer.html" %>
5 <jsp:forward page="disclaimer.html" />


I can't image any reason why 3 is not right, and can't image any reason 5 is right. So, my answer is 1 & 3.
I tested two files on Tomcat 5.0.16:
one is include.jsp:

<%@include file="/header.jspf"%>
include.jsp -- 1
<jsp:include page="tt.txt" flush="true"/>
include.jsp -- 2
<%@include file="/footer.jspf"%>

the tt.txt is:

This is the output from tt.txt!

the result for requesting include.jsp is:

include.jsp -- 1 This is the output from tt.txt! include.jsp -- 2

[ January 17, 2004: Message edited by: hover cheng ]
 
Kelvin Hung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hover,
I just tested the code. I think you are right. So are you saying that anything before <jsp:forward> is discarded and the page is forwarded to the new page?
By the way, for question 1) your answer is 1 and 3, but 1 is <jsp:include file="disclaimer.html" />, it should be "page". I am sure you just overlooked it.
Also, do you have any recommendation on SCWCD mock test?
Kelvin
 
hover cheng
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So are you saying that anything before <jsp:forward> is discarded and the page is forwarded to the new page?

To clear this out, the Servlet2.4 specification says:

SRV.8.4 The Forward Method
The forward method of the RequestDispatcher interface may be called by the
calling servlet only when no output has been committed to the client. If output data exists in the response buffer that has not been committed, the content must be cleared before the target servlet´┐Żs service method is called. If the response has been committed, an IllegalStateException must be thrown.


By the way, for question 1) your answer is 1 and 3, but 1 is <jsp:include file="disclaimer.html" />, it should be "page". I am sure you just overlooked it.

yeap, sorry for that.

Also, do you have any recommendation on SCWCD mock test?

Hmmm, this is frustrating me. I haven't found any good mock test... I just took some free mock test(linked from http://www.javaranch.com/carl/scwcd/scwcd_mock_logo.jsp), but it seems not include filter, simple tage handler, el and tag file... Do you have some recommendation for me? Thanks in advance.
 
Kelvin Hung
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only one I took is at
http://j2eecertificate.com/
It is not that bad. Most of the questions make sense. Those don't make sense have already been asked by me on this forum.
Do you have the Wrox SCWCD book? It has a mock test. I can send it to you if you want it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic