javaservletsservercross-contextserver-application

Obtain servlet response when using Cross-Context


I'm using cross-context to call a servlet in another server application: Servlet /bar from server application 'A' calls /foo servlet on server application 'B'.

I'm using this very nice solution, just as in the Abhijeet Ashok Muneshwar answer, I forward the request from server application A to the /foo servlet on server application B.

I'm using the class RequestDispatcher () to send a request, but the response is returned in the same call?

RequestDispatcher rd = context.getRequestDispatcher("/Servlet2");
rd.forward(request, response);

How can I process and return the response from server application B in A's servlet.

Thanks.


Solution

  • If you use a forward, that passes control to the target of the forward. The other option with a RequestDispatcher is to do an include.

    If you want more control than that, you'll have to use an HTTP client to retrieve the response and then apply whatever processing you want to but using an HTTP client this way is not something I'd recommend. You'd be better off refactoring your application so you can use RequestDispatcher.include.