Calling a servlet from another servlet | Community
Skip to main content
sravs
Community Advisor
Community Advisor
July 29, 2022
Solved

Calling a servlet from another servlet

  • July 29, 2022
  • 2 replies
  • 6375 views

Hi everyone, 

 

Is there any possibility that we can call a servlet from another servlet after receiving the response from the first servlet.

 

Note: Both the servlets are of POST method and both are of Path based servlets.

 

Please provide your inputs on this.

 

Regards,

Sravani

 

 

Best answer by GanthiRa1

@sravs 

You can call the servlet programmatically from another servlet in one of two ways, as per [1].

  • To include another servlet's output, use the include() method from the RequestDispatcher interface. This method calls a servlet by its URI and waits for it to return before continuing to process the interaction. The include() method can be called multiple times within a given servlet.

    For example:


   RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/servlet/ShowSupplies");
    dispatcher.include(request, response);
  • To handle interaction control to another servlet, use the RequestDispatcher interfaces forward() method with the servlet's URI as a parameter.

    This example shows a servlet using forward():


   RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/servlet/ShowSupplies");
    dispatcher.forward(request, response);

         

 

Here is the reference

[1] https://docs.oracle.com/cd/E19146-01/819-2634/abxbn/index.html

[2] https://www.geeksforgeeks.org/servlet-collaboration-java-using-requestdispatcher-httpservletresponse/

 

Hope this help!

2 replies

GanthiRa1Community AdvisorAccepted solution
Community Advisor
July 29, 2022

@sravs 

You can call the servlet programmatically from another servlet in one of two ways, as per [1].

  • To include another servlet's output, use the include() method from the RequestDispatcher interface. This method calls a servlet by its URI and waits for it to return before continuing to process the interaction. The include() method can be called multiple times within a given servlet.

    For example:


   RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/servlet/ShowSupplies");
    dispatcher.include(request, response);
  • To handle interaction control to another servlet, use the RequestDispatcher interfaces forward() method with the servlet's URI as a parameter.

    This example shows a servlet using forward():


   RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("/servlet/ShowSupplies");
    dispatcher.forward(request, response);

         

 

Here is the reference

[1] https://docs.oracle.com/cd/E19146-01/819-2634/abxbn/index.html

[2] https://www.geeksforgeeks.org/servlet-collaboration-java-using-requestdispatcher-httpservletresponse/

 

Hope this help!

arunpatidar
Community Advisor
Community Advisor
July 30, 2022

Hi,

Generally we don't do that. The best way to handle this kind of situation to encapsulate the logic in osgi service/component and reference these services in servlet.

So no need to make additional request.

Now Your servlet can refer both services 

Arun Patidar