This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward + queryString

 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 SourceServlet.java and DestineServlet.java.
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:





Thanks!!
 
Niiraj Patel
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic