0

Possible Duplicate:
How do I execute a sequence of servlets?

I'd like to do something that seems to this, you click a button (POST method) -> call servlet1 (treatment 1) -> call servlet2 (treatment2) -> call servlet3 (treatment3 ).....

The second Servlet must await the completion of the first servlet and so on.

Community
  • 1
  • 1
YAKK
  • 1
  • Duplicate of [How do I execute a sequence of servlets?](http://stackoverflow.com/questions/3024949/how-do-i-execute-a-sequence-of-servlets) – BalusC Jan 23 '11 at 18:30

3 Answers3

0

Can you forward/redirect to the second servlet and chain them that way? If not, I recommend refactoring to have them call methods rather than separate servlets.

Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
  • Can you explain me how to forward/redirect to another servlet? I think we can only redirect to jsp and not servlet – YAKK Jan 23 '11 at 18:15
  • this posts shows how to use RequestDispatcher to do a forward: http://java.boot.by/wcd-guide/ch03s05.html – Aravind Yarram Jan 23 '11 at 18:21
  • Right. And I should also mention you use a forward when you want it as part of the same logical unit. This shares the request parameters. You use a redirect when you want to "break" it and have a browser refresh call only the last servlet. – Jeanne Boyarsky Jan 23 '11 at 18:23
  • If you really need to, use don't use forward, but use include. See also the linked dupe. – BalusC Jan 23 '11 at 18:35
0

I think you need RequestDispatcher's forward method:

http://download.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html#forward(javax.servlet.ServletRequest,%20javax.servlet.ServletResponse)

Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46
0

This "chain of responsibility" design is provided out-of-the box with servlet filters, instead of servlets. This way, instead of explicitely forwarding to the next servlet using a RequestDispatcher, you simply declare the filters in the desired order in web.xml and be able to simply add more or remove as needed (without touching the existing code).

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92