JSP to Servlet Relation

六月ゝ 毕业季﹏ 提交于 2020-01-01 10:45:53

问题


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.


回答1:


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.




回答2:


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



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/3198703/jsp-to-servlet-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!