JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Wednesday May 28th 2003 Wednesday May 28th 2003

Why might you use the method HttpServletResponse.encodeRedirectURL instead of HttpServletResponse.encodeURL? (1 correct answer)



A Because your servlet or jsp code includes a call to the RequestDispatcher.forward() method.
B Because your servlet or jsp code includes a call to the RequestDispatcher.include() method.
C Because your servlet or jsp code includes a call to the HttpServletResponse.sendRedirect() method.
D Because your session management relies on URL rewriting rather than cookies.
E Because the HttpServletResponse.encodeURL method does not exist.
Page down for the answer...











































The Answer

C is the correct answer. The advice is to always pass a URL through encodeRedirectURL() when preparing it as a parameter to the HttpServletResponse.sendRedirect() method. This is because - allegedly - redirect URLs have different encoding requirements to normal URLs.
A and B are wrong because forward() and include() from the RequestDispatcher happen on the server side. There is no need to return a session id to the client, so no need to encode it. (sendRedirect(), on the other hand, does return control back to the browser).
D is a correct statement, but does not explain why one method is to be preferred over the other.
And E is just a direct lie to persuade you to doubt that there really are two separate methods for encoding URLs.


EMail: dbridgewater@jbridge.co.uk
Phone: +44 (0)1943 877414
Fax: +44 (0)1943 877414
Mail: David Bridgewater, Willow Dene, Bradford Road, Menston, Ilkley, West Yorkshire, LS29 6ED, UK
Copyright © 2003 David Bridgewater. All rights reserved.