Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP to Servlet Relation

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.

like image 509
Kevin Hicks Avatar asked Jul 07 '10 20:07

Kevin Hicks


2 Answers

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.

See also:

  • What's the difference between Servlet, JSP and JSF?
  • What's the mainstream Java alternative to ASP.NET/PHP?
  • Things you should know about JSP/Servlet
like image 149
BalusC Avatar answered Oct 12 '22 22:10

BalusC


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.

like image 45
zengr Avatar answered Oct 12 '22 22:10

zengr