• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, sorry just i want to remember it. still i confused about this post
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
--------------------------------------------------------------------

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cesar,
Could you please post the result.
 
cesar valencia
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
knowledge is the difference between drudgery and strategic action -- tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic