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!
Â
Â
Â