简体   繁体   中英

servlet calling and getting result from another servlet

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

eg

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM