N/APosted on - 09/30/2011
What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?
Explain the life cycle methods of a Servlet
Request Dispatcher can be retrieved by 2 methods
1. From a ServletRequest
RequestDispatcher view = request.getRequestDispatcher(?mPage.jsp?);
The getRequestDispatcher() method in ServletRequest takes a String path for the resource to which you are forwarding the request. If the path starts with a forward slash (?/?), the Container sees that as "starting from the root of this web app". If the path does NOT start with a forward slash, it's considered relative to the original request.
2. From ServletContext
RequestDispatcher view = getServletContext().getRequestDispatcher(?/mPage.jsp?);
Like the equivalent method in ServletRequest, this getRequestDispatcher() method takes a String path for the resource to which you're forwarding the request, EXCEPT you cannot specify a path relative to the current resource (the one that received this request). That means you must start the path with a forward slash!