JBridge

JBridge: Certification Question Of The Day

JBridge Home >> Certification Questions >> Question for Friday 9th May 2003 Friday 9th May 2003

Given a web application called “Certification” hosted on a Windows platform, which path retrieval methods in the servlet API are likely to return the following String?
C:\Java\jakarta-tomcat-4.1.18\webapps\Certification
(2 correct answers)



A HttpServletRequest.getPathInfo()
B HttpServletRequest.getPathTranslated()
C HttpServletRequest.getRealPath()
D HttpServletRequest.getRealPath(“”);
E ServletContext.getRealPath(“”);
F HttpServletRequest.getServletPath()
G HttpServletRequest.getContextPath();
Page down for the answer...











































The Answer

The correct answers are D and E. Though HttpServletRequest.getRealPath(String path) is deprecated, it will give the result required. By feeding this method a blank String, the full machine path up to and including the web application name is returned. The non-deprecated method ServletContext.getRealPath() does the same job.
Answer C is wrong because HttpServletRequest.getRealPath() must receive a String as a parameter; there is no overloaded version without any parameters.
HttpServletRequest.getPathInfo() (answer A) has to do with retrieving information from the URL following the part of the URL which maps to a resource (such as a servlet).
HttpServletRequest.getPathTranslated() (answer B) does a similar job, and is likely to return more than the question answer requires – for example, C:\Java\jakarta-tomcat-4.1.18\webapps\Certification\MyServlet\mypathinfo
HttpServletRequest.getServletPath() and getContextPath() are both perfectly valid methods, but don’t produce the result required (they might respectively return, for example, “/MyServlet” and “/Certification”).


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.