Wednesday, May 28, 2008

Java Tricky Question - 3

Both the interfaces ServletContext and ServletRequest have method getRequestDispatcher() .. whats the difference between them ??

Ans :
You can pass a relative path to the getRequestDispatcher() method of ServletRequest but not to the getRequestDispatcher() method of ServletContext. For example, request.getRequestDispatcher("../html/somePage.html") is valid, and the getRequestDispatcher() method of ServletRequest will evaluate the path relative to the path of the request. For the getRequestDispatcher() method of ServletContext, the path parameter cannot be relative and must start with /. This makes sense because ServletRequest has a current request path to evaluate the relative path while ServletContext does not.

No comments: