0

I have been trying to forward a request from servlet1 to servlet2, which I was able to do successfully using RequestDispatcher.forward(request, response), but this way it prints the output from servlet2. I want to get output from servlet2 in servlet1 and manipulate it further. I have tried to use RequestDispatcher.include(request, response) but it doesn't get to servlet2 for some reasons. Is there any way to get the output from servlet2 in servlet1 and filter it out? Note: I am writing a jsonObject.toString() to the response of servlet2

Purva
  • 1
  • 1
  • RequestDispatcher.forward() is a redirect from server-side keeping all POSTed inputs to be processed on the 2nd servlet. It sounds like RequestDispatcher.include() is what you need. Here is a good example of how .include() works. http://www.thejavageek.com/2013/12/11/requestdispatcher-include-method/ – Minh Kieu May 18 '17 at 15:53
  • It might be useful to refactor your code such that business logic is executed in separate business classes that are called from each servlet independently. – John May 18 '17 at 16:14
  • @MinhKieu I have tried using RequestDispatcher.include() but no matter what it does not hit the second servlet. In same place, if I use RequestDispatcher.forward() it works fine. – Purva May 18 '17 at 18:19
  • @John In my case servlet1 is in different context than servlet2 and servlet2 is responsible for collecting all data and should be returning it to servlet1 for filtering that data. What exactly do you suggest? – Purva May 18 '17 at 18:22
  • I would tend to implement each of those tasks (find data, filter data) in business classes (plain old java objects -- POJOs) and not in servlet classes. For example a class that's called something like DataFactory that has a method getData() and a class that's called something like DataFilterer that has a method filterData(List data). The servlet that gets the data would then have two lines of code that calls each of these methods respectively. – John May 19 '17 at 19:00

0 Answers0