Expand my Community achievements bar.

SOLVED

Calling a servlet from another servlet

Avatar

Level 9

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

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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!

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

@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!

Avatar

Community Advisor

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