3 Replies Latest reply: Dec 17, 2012 10:49 PM by rp0428 RSS

    How Many Controller classes can I Define?

    800433
      Hi All...
      I am Developing one MVC based web application using Servlets and JSP. I have red about MVC pattern that, there must be only one controller servlet which call all models or dao classes. But Can I use more than one servlet as Controller class in my web application? Because I am not using any Frameworks like Struts. Please suggest me good design for my application.
      Thanks in advance...
        • 1. Re: How Many Controller classes can I Define?
          jduprez
          user10137999 wrote:
          Hi All...
          I am Developing one MVC based web application using Servlets and JSP. I have red about MVC pattern that, there must be only one controller servlet which call all models or dao classes.
          You've probably misread that part.
          The usual advice is, for a given interaction, to keep business logic (e.g. invoking Model's business methods such as search, store, compute,...), and flow logic (go to this or that page according to the value of such or such value returned by the business logic) in the controller servlet, and leave the JSP do mere rendering (calling a getXxx method is generally OK there).
          But Can I use more than one servlet as Controller class in my web application?
          Sure. Often a single servlet is used throughout a given flow (e.g. search train + select + pay, may all go through the same TrainBookingServlet , this latter doing the appropriate model calls according to the current step of the flow). But it's also entirely possible to have one servlet for each action SearchServlet , SelectSeatServlet, PayServlet .
          Even with "one servlet serving all steps of a given flow", if you application has several flows (e.g. "book", but also "edit preferences", "Register Credit Card",...), it's probably a better idea (easier maintenance) to have different Servlets.
          Because I am not using any Frameworks like Struts. Please suggest me good design for my application.
          You don't have to follow framework, especially if you're learning. However, keep in mind that the various existing frameworks:
          - promote a consistent design approach (not consistent across frameworks, but within a given framework, and design consistency tremendously helps maintenance)
          - have built-in or known solutions to recurring problems (performances, concurrency, handling the "back" button,...) that your ad-hoc solution may not address easily.
          So once you have learnt (and got burnt :o) ), it may pay off to study the various frameworks and select one that fits most of your needs.

          Best regards,

          J.
          • 2. Re: How Many Controller classes can I Define?
            800433
            Thank you jduprez. Now I have got idea using good MVC design. Credit goes to you. Good explanation.
            • 3. Re: How Many Controller classes can I Define?
              rp0428
              Don't be shy about giving him the point credits for helping you.