probuddha singha
probuddha singha

Reputation: 97

Which class provides the implementation for getRequestDispatcher() method

The getRequestDispatcher() method of ServletRequest interface returns the object of RequestDispatcher.

I know that getRequestDispatcher() method is in the Interface ServletRequest. As it is an interface, it won't define the method.

Furthermore, this interface is again inherited in another interface HttpServletRequest. But being an interface, it won't define its methods.

Now, after carefully checking the JavaDocs, I could not find any class that implemented either of these two interfaces, and defined the getRequestDispatcher() method.

So I was wondering where did they define this method

Thanks in advance.

Upvotes: 4

Views: 1135

Answers (3)

msangel
msangel

Reputation: 10362

For jetty v6 it is org.mortbay.jetty.servlet.ServletHandler and its return org.mortbay.jetty.servlet.Dispatcher instance.

And this is how forward from Dispatcher works there:

  1. It took original request, replace request url with new value
  2. Clear output stream.
  3. Execute request-responce pair via context.handle(request, responce)
  4. Reset original value of request url for request.

Upvotes: 0

AnkeyNigam
AnkeyNigam

Reputation: 2820

The class which implements is org.apache.catalina.connector.RequestFacade , in case of TOMCAT container. The implementation is basically dependent on containers & every container has its own implementation adhering to the J2EE Specs.

Use the below code to check the implementation class :-

public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
            throws ServletException, IOException {
        System.out.println(httpServletRequest.getClass());
    }
}

Output :- org.apache.catalina.connector.RequestFacade

You can see this class Offical Doc here, and can check that it has implemented the interface javax.servlet.ServletRequest and its methods like getRequestDispatcher() etc.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691715

Each container (Tomcat, Jetty, etc.) has its own class that implements HttpServletRequest. This class implements all the methods of the interface. An instance of this class is passed by the container to your servlet.

If you're really curious about it, add

System.out.println(request.getClass());

to your servlet, and you'll know the name of the concrete class.

But really, you don't need to know about it. All you need to know is that it implements the HttpServletRequest interface and thus fulfills the contract described in the javadoc and the specifications.

Upvotes: 3

Related Questions