Explain the life cycle methods of a Servlet

Asked By 0 points N/A Posted on -
qa-featured

What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?

SHARE
Answered By 0 points N/A #83969

Explain the life cycle methods of a Servlet

qa-featured
 
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!
 
 
 

Login/Register to Answer

Related Questions