File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes [HFSJ 2nd Edition] chapter 5 Mock Exam Question # 12 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 "[HFSJ 2nd Edition] chapter 5 Mock Exam Question # 12" Watch "[HFSJ 2nd Edition] chapter 5 Mock Exam Question # 12" New topic
Author

[HFSJ 2nd Edition] chapter 5 Mock Exam Question # 12

Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
I have completed chapter 5 and while taking the mock exam i couldn't answer few questions.
Which statements about RequestDispatcher are true (where applicable, assume the RequestDispatcher was not obtained via a call to getNamedDispatcher())? (Choose all that apply.)
A. A RequestDispatcher can be used to forward a request to another servlet.
B. The only method in the RequestDispatcher interface is forward().
C. Parameters specified in the query string used to create a RequestDispatcher are not forwarded by the forward() method.
D. The servlet to which a request is forwarded may access the original query string by calling getQueryString() on the HttpServletRequest.
E. The servlet to which a request is forwarded may access the original query string by calling getAttribute(�javax.servlet.forward.query_string�) on the ServletRequest.

Option A and E are the correct one.My question is How a RequestDispatcher can be used to forward a request to another servlet? As this is not explained in the book I couldn't answer it correctly.
Shilpa Shandilya
Greenhorn

Joined: Dec 04, 2008
Posts: 2
Vishal,

You can use a RequestDispatcher to forward/include a request to another servlet or jsp or html resource. When you want to forward the request to a servlet, you just need to mention its path in the getRequestDispatcher() method.

Please see this sample code to understand better.
http://www.stardeveloper.com/articles/display.html?article=2001062401&page=1
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Ok! Now I know how to call servlet from servlet.
Back to quetion, The answer is option A and E.I tried testing option D and E. But I got correct query string value when I executed,

And I got null when I did,

So, Doesn't that mean option D is correct and option E is incorrect.
Still the picture is fuzzy.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Vishal Pandya:
Still the picture is fuzzy.


yes. i also tried. i get output as null for request.getAttribute("javax.servlet.forward.query_string")

any update?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hi, sorry just i want to remember it. still i confused about this post
cesar valencia
Ranch Hand

Joined: Oct 14, 2008
Posts: 33
you should try it yourself, i'll post some sample code:






Now, try invoking the servlet with a few parameters, say inicio.do?param=something¶m2=whatever, you'll see both getQueryString and getAttribute("javax.servlet.forward.query_string") give the same result. This is sort of acknowledged in the non-official errata for HFSJ:


--------------------------------------------------------------------

[221] Answer D of question 12.;
The text indicates answer D (which refers to
HttpServletREquest.getQueryString) is not correct and answer E (which refers to javax.servlet.forward.query_string) is correct. However, according to page of 62 of version 2.5 (MR6) of the Servlet Spec "servlet-2_5-mrel2-spec.pdf":


"
...
javax.servlet.forward.query_string

The values of these attributes must be equal to the return values of the
HttpServletRequest methods ..., getQueryString, respectively, invoked on the request object passed to the first servlet object in the call chain that received the request from the client. ..."

Therefore, either D and E are both correct or they are both incorrect, and it is clear from the spec that they are

Author comment:
Needs more research
--------------------------------------------------------------------


scjp5, scwcd5
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Cesar,
Could you please post the result.


SCJP, SCWCD, SCBCD
cesar valencia
Ranch Hand

Joined: Oct 14, 2008
Posts: 33
Originally posted by Deepak Jain:
Cesar,
Could you please post the result.


sure thing, Deepak

in the tomcat log:


and in the jsp:


as you can see, it outputs the same thing. Here's the war (with src) if you wanna try it yourself: http://zomgwtfbbq.org/ranch/forwardRequest.war
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637


What happens if you replace
RequestDispatcher reqDispatch = getServletContext()
.getRequestDispatcher("/siguiente.do");

with
RequestDispatcher reqDispatch = getServletContext()
.getRequestDispatcher("/siguiente.do?newParam=test");

The place am sitting does not have tomcat container and hence i cannot test it out myself.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by cesar valencia:
you should try it yourself, i'll post some sample code:




above one is my servlet


if you call the servlet by http://localhost:8080/webapp/bear?seethu=ram

the output is

-----------------------------------------------------
QUERY STRING---->seethu=ram
QUERY STRING FORWARD---->null
-----------------------------------------------------

i agree,what you are giving the example is ok.. but if you directly call the servlet by passing the query string ,i am not getting the desire ouput.

what do you say about this ? any reason for this..please explain me
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
http://localhost:8080/forwardRequest/Test1.jsp?param=something&m2=whatever



1) Forward to Test2.jsp without passing any parameters to Test2.jsp inside RequestDispatcher.getRequestDispatcher()
2) Inside Test1.jsp a call to
System.out.println("New current querystring: " + request.getQueryString());
will display
param=something&m2=whatever

because Test1.jsp was invoked using the above query string, If no query String is used then it will return null.

3) Inside Test2.jsp

out.println("getQueryString: " + request.getQueryString());
Will result in : the query string that was used to invoke Test2.jsp. In Test1.jsp since we did not pass a new query string while getting Request dispatcher to Test2.jsp, the same query string from Test1.jsp is passed to Test2.jsp
Hence the output to above statement is aram=something&m2=whatever
Further:
out.println("<br/>jsp attribute:: " + request.getAttribute("javax.servlet.forward.query_string"));
Will anyways display the Query string that was used by Test1.jsp [The invoker Query String] : Hence the output to above statement is :
param=something&m2=whatever

Now comes the big change:
Test1.jsp

Test2.jsp: Remains Same
The result now:
In Test1.jsp:
System.out.println("New current querystring: " + request.getQueryString());
aram=something&m2=whatever
Which is expected.
Now in Test2.jsp [On browser]
getQueryString: newParam=test :
jsp attribute:: param=something&m2=whatever

getQueryString: Always gets the query string of the invoker servlet/JSP if the query string is modifed by invoker before forwarding the request then it will display the new query String.
javax.servlet.forward.query_string : Request Attribute will always have the query string of the invoker [the one who forwards the request].


I used JSP because its much easier to edit. Further JSP are servlets.
I hope this will clear the confusion.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
With above knowledge lets try to answer the original question
A RequestDispatcher can be used to forward a request to another servlet.
This is completely wrong since a request dispatcher can forward requests to servlet, JSP file, or HTML file on the server.
Hence A is wrong.


B. The only method in the RequestDispatcher interface is forward().
This is completely wrong since a RequestDispatcher has forward() and include() methods
Hence B is wrong.


C. Parameters specified in the query string used to create a This is wrong. If parameters are specfied in the getRequestDispatcher() of the destination source then those parameters will be passed to the destination resource and the query string [or parameters] of the source will not be passed.

Hence C is wrong.


D. The servlet to which a request is forwarded may access the original query string by calling getQueryString() on the HttpServletRequest.

This is wrong. If parameters are specfied in the getRequestDispatcher() of the destination source then those parameters will be passed to the destination resource and the query string [or parameters] of the source will not be passed. Once inside Destination resource a call to getQueryString() will get the new query String that was used to invoke the destination resource.

Hence D is wrong.

E. The servlet to which a request is forwarded may access the original query string by calling getAttribute(�javax.servlet.forward.query_string�) on the ServletRequest.
E is correct choice as my previous post demonstrated this using an example.
Hence E is correct.
So the correct answer to the question is E .
[ December 08, 2008: Message edited by: Deepak Jain ]
cesar valencia
Ranch Hand

Joined: Oct 14, 2008
Posts: 33
Originally posted by Deepak Jain:
What happens if you replace
RequestDispatcher reqDispatch = getServletContext()
.getRequestDispatcher("/siguiente.do");

with
RequestDispatcher reqDispatch = getServletContext()
.getRequestDispatcher("/siguiente.do?newParam=test");[/QB]

it outputs:

which is expected, since the queryString for the current servlet has changed, but remains the same for the attribute

Originally posted by seetharaman venkatasamy:
i am not getting the desire ouput.

what do you say about this ? any reason for this..please explain me

that would be due to not actually forwarding the response to another servlet. Since you're directly accessing the second servlet, you've effectively lost the
javax.servlet.forward.query_string attribute, but you've still have a query string.



edit:
Deepak beat me to it . Anyway, about this post...
Originally posted by Deepak Jain:
With above knowledge lets try to answer the original question
A RequestDispatcher can be used to forward a request to another servlet.
This is completely wrong since a request dispatcher can forward requests to servlet, JSP file, or HTML file on the server.
Hence A is wrong.

I would be inclined to believe that it is a correct statement since it doesn't say that you can only forward to a servlet, it only implies that you could (which you do can). So i would still mark this as correct.

Originally posted by Deepak Jain:
D. The servlet to which a request is forwarded may access the original query string by calling getQueryString() on the HttpServletRequest.

This is wrong. If parameters are specfied in the getRequestDispatcher() of the destination source then those parameters will be passed to the destination resource and the query string [or parameters] of the source will not be passed. Once inside Destination resource a call to getQueryString() will get the new query String that was used to invoke the destination resource.

Hence D is wrong.

I agree, but this option could be better stated as "The servlet to which a request is forwarded may access the original query string by calling getQueryString() on the HttpServletRequest, as long as no new parameters in the target URL are set". This would make it a true statement.
[ December 08, 2008: Message edited by: cesar valencia ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: [HFSJ 2nd Edition] chapter 5 Mock Exam Question # 12