开发者

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 do开发者_运维技巧n'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.

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


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.


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).


  • 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜