- getRequestDispatcher(path): In order to create it we need to give the relative path of the resource
- getRequestDispatcher(path):In order to create it we need to give the absolute path of the resource.
In Java EE (Enterprise Edition) or Jakarta EE, the request.getRequestDispatcher()
and context.getRequestDispatcher()
methods are used to obtain a RequestDispatcher
object, but they are associated with different scopes.
request.getRequestDispatcher()
:- This method is called on an instance of
HttpServletRequest
. - The
RequestDispatcher
obtained using this method is used for request-forwarding to resources that are relative to the current request.
Example:
javaRequestDispatcher dispatcher = request.getRequestDispatcher("/somePage.jsp");
dispatcher.forward(request, response);
In this case, the forward is relative to the current request.
- This method is called on an instance of
context.getRequestDispatcher()
:- This method is called on an instance of
ServletContext
. - The
RequestDispatcher
obtained using this method is used for request-forwarding to resources that are application-wide or not relative to a specific request.
Example:
javaRequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/somePage.jsp");
dispatcher.forward(request, response);
In this case, the forward is not tied to a specific request and is typically used for application-wide resources.
- This method is called on an instance of
In summary, request.getRequestDispatcher()
is used for relative resource paths within the current request, while context.getRequestDispatcher()
is used for paths that are not tied to a specific request and are typically application-wide.