0

I would like to start a thread on handling the "multipe Form, one Servlet" scenario and discuss pros and cons. There are multiple use cases where this model could be deployed with a prime example being:

AccountDetails.jsp : contains multiple forms UpdateAccountDetalsServlet : depending on which form was submitted, calls a DAO method to update the database.

Now the obvious solution here would be to pass a hidden parameter to the servlet and determine which form was submitted, but this doesn't feel right. Why?

I'd like to get some feedback.

Thanks.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
s.d
  • 87
  • 2
  • 6
  • Related: http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/ – BalusC Dec 27 '10 at 23:38

1 Answers1

1

Yes, that is fine. You can even use enums:

OperationType opType = OperationType.valueOf(request.getParameter("opType"));

switch(opType) {
  case SAVE..
  case DELETE..
}

In Spring MVC prior to the new restful model you could have a multi-action controller. There you had to pass a parameter in the URL, like method=save, and spring invoked the save() method on your object. This is something you can also implement, but it includes reflection.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks - I am currently using enums actually. Would you rather use enums instead of one servlet per form/action ? – s.d Dec 28 '10 at 01:49
  • I personally would use a web framework like Spring MVC. But if not - yes, I'd use an enum. Actually - take a look at BalusC link (http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/) – Bozho Dec 28 '10 at 09:20