There are many, many examples in books, and on the internet about how to use Servlets as JSPs. But I would like to know what the best way to go about using them, with a mind to good architecture.
Should there be a one-to-one relation of Servlets to JSPs? Acting like ASP.NET "Code-Behind" pages?
Or more like ASP.NET MVC, with a single Servlet controlling multiple actions, and forwarding to multiple views?
This is a question regarding pure Java EE development. Please don't simply suggest another framework.
Should there be a one-to-one relation of Servlets to JSPs? Acting like ASP.NET "Code-Behind" pages?
Depends. It's affordable for a small website with maybe 3~5 pages, but above that it's going to generate a lot of boilerplate code that you'll almost end up with a homegrown MVC framework when refactoring all that duplicate code yourself in a sensible manner.
Or more like ASP.NET MVC, with a single Servlet controlling multiple actions, and forwarding to multiple views
That's more recommendable when having a web application of a bit decent size. The Java counterpart of ASP.NET MVC is by the way JSF (JavaServer Faces). It's a pure Java EE provided component based MVC framework which supplies the FacesServlet
as the sole controller so that you can end up with just a Javabean class as model and a JSP (or, more recently) Facelets page as view. Facelets? Yes, since JSF 2.0, the vintage JSP has been replaced by Facelets as default view technology. Facelets is XHTML based.
If you'd like to homegrow a controller servlet, then check the front controller pattern. You can find another basic kickoff example in this answer.
How about this? I made this in one of my school projects:
alt text http://img576.imageshack.us/img576/3064/mvci.jpg
This was my assumption based on my understanding of servlets and JSP. I would love to have your comments and ideas to improve this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With