1

I am trying to get the result from servlet (B) within servlet (A) and react to the result.

e.g.

in servlet B

response.getWriter().print("HELLO");

in servlet A

ServletContext context = this.getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/B");
dispatcher.include(request, response);

// if the response from B is "HELLO", then do something

What would be the best practice for the solution? or should the way it response in servlet B would be changed to other method?

Gladhus
  • 910
  • 1
  • 13
  • 24
user2625363
  • 845
  • 2
  • 10
  • 15
  • Can't you simply move the generation of "HELLO" to common component both servlets would use? – Gedrox Apr 09 '14 at 11:31
  • 2
    You are most likely missusing the concept of servlets. If you explain what is the broader context we might suggest better solution. – Jan Zyka Apr 09 '14 at 11:50
  • 1
    as I said, I just want to fetch some result from another sevlet and react to it – user2625363 Apr 09 '14 at 12:23

1 Answers1

0

you should use this method:

  1. use a RequestDispatcher
  2. use a URLConnection or HTTPClient
  3. send a redirect
  4. call getServletContext().getServlet(name)

Calling Servlet Post from another Servlet

Community
  • 1
  • 1
jmail
  • 5,944
  • 3
  • 21
  • 35