How do I use a RequestDispatcher to call one servlet from another servlet?

André Wolf

To call another servlet you have to use a request dispatcher:

RequestDispatcher rd =
    this.getServletConfig()
        .getServletContext()
        .getRequestDispatcher("/path-to-servlet");
rd.forward();
or
rd.include();
See J2EE-API documentation for more information.

Note that you cannot use a full qualified URL in getRequestDispatcher(). This must be the path of the servlet relative to the servlet context. For example: If your servlet context is called "/Select" and your servlet (which you want to dispatch to) is mapped to "/Select" within this context, you have to call getRequestDispatcher("/Select?TABLA="+param).

You should also verify if the returned dispatcher object is not null before you call either include(request, response) or forward(request, response). As you can see in the API documentation both methods require two parameters request and response, which are the same one you have defined in doGet().

I recommend you to read the API documentation, everything is very well explained in there. And I can recommend the Servlet Tutorial you can find at the SUN-Java webpage, especially the section "Invoking Other Web Resources".

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact