views:

82

answers:

4

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 JEE development. Please don't simply suggest another framework.

Thank you

A: 

No, a one-to-one relationship from Servlets to JSP is not strictly necessary since they are different things.

What I personally like is using Servlets as Controllers, and then, after processing the HTTP REQUEST, render the response (HTML) with one or more JSP page. The ASP.NET "Code-Behind" does not apply, since CODE-BEHIND is a one to one relationship with its corresponding ASPX file. AFAIK, you can't render one ASP.NET "code behind" with different ASPX pages.

Anyway, it's not an easy question, but in my experience, just plain old Servlets+JSP approaches is usually cleaner, simpler and less buggy than any other framework build on top of them (Struts, JSF and many others).

Pablo Santa Cruz
+2  A: 

How about this? I made this in one of my school projects:

alt text

This was my assumption based on my understanding of servlets and JSP. I would love to have your comments and ideas to improve this.

zengr
Thanks. Love the graphic too. So, since everything comes through the same doPost/doGet method, how in your Servlet do you route different actions? Do you use URL parameters? Perhaps with URL rewriting? Or do you read the requested path?
Kevin Hicks
I read the requested path and delegate accordingly.
zengr
This is exactly what I was thinking, but I wanted to ask an experienced community first. Thank you again!
Kevin Hicks
Yup, I am also interested in what the experienced guys think. There are many "theoretical" answers out there though :P
zengr
A: 
  • JSPs are servlets (in disguise). Each jsp is transformed and compiled as servlet. Look at the /work directory of your tomcat for exmaple
  • JSPs are a view technology - i.e. they make it easier to write reusable pages
  • JSPs should be used only to display the results that a regular servlet has pre-computed, placed inside a request/session, and forwarded to the corresponding jsp.
Bozho
Thank you for completely missing the point, and not answering my question.
Kevin Hicks
@Kevin Hicks: Perhaps you have completely missed Bozho's point.
Don Roby
@Kevin Hicks eh, I answered your question twice - in the 1st and 3rd bullet.
Bozho
@downvoter - would you also explain ?
Bozho
+2  A: 

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:

BalusC