File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes forward + queryString 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 "forward + queryString" Watch "forward + queryString" New topic

forward + queryString

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

When solving the Exercise 12 from chapter 5 of Head First Servlets & JSP, the statement below was said to be wrong.
"The Servlet to which a request is forwarded may access the original query string by calling getQueryString() on the HttpServletRequest."
It's said in the book that this is incorrect because this method returns the query string on the URL pattern from the RequestDispatcher.

When trying by coding an example I've seen that the statement above is true and not wrong as said the book.
I've created two Servlets called and
The SourceServlet is called with a GET method from a link from any html page as
<a href="SourceServlet?name=adscript">Link</a>

When checking the query string data at SourceServlet, those data is there as expected.
So I've coded a dispatching to DestineServlet to check there for URL parameters and queryString invocation method.
The result was as I've expecting: when invoking the getQueryString method, I've got the value name=adscript.

So I'd like to know if I'm interpreting this exercise wrong, or it's an error at the answer provided by HF Servlets & JSP.

Note: I'm reading the second edition. At the first edition, the statement was said to be true.

Code example:


Adolfo Eloy
Software Developer
Niiraj Patel
Ranch Hand

Joined: Sep 17, 2011
Posts: 47
I have altered your Servlets little to get rid..

modify query string when you forward.

Print both original param. of SourceServlet and forwarded request's parameter.

Now, you will able to see two different queries..
1.query string Original:name=adscript
2.query string forwarded:name=newQueryParam

Conclusion : forward/include methods first store original query string value in attribute and set new query string to new request object.

I agree. Here's the link:
subject: forward + queryString
It's not a secret anymore!