In one of the interviews i attended i was asked "why is JSP used when ultimately its translated to a servlet again, why not just do servlet programming?" or to be more clear, "What factor decides the choice of JSP or servlets in a project?". My explanation of MVC didn't satisfy them. Please help me with more appropriate answer.
plus JSP is all tag-based. So, someone can come up with a RAD GUI toolkit that can quickly create JSP pages by dragging-dropping HTML as well as custom tags on the page.
Joined: Oct 12, 2000
no, JSP is not all tag based. Though it's perfectly possible to write JSPs without a line of Java code in them this wasn't always the case and you'll still find a massive volume of JSPs that have no JSTL or custom tags at all (or use a combination of JSTL, custom tags, and Java code).
There is in a good system no "only JSP" or "only Servlets", the two technologies are meant to integrate seamlessly. JSPs are excellent for output generation and (when using JSTL or custom tags) can be maintained by people with minimal Java knowledge if those people are given a description of the data passed into the JSP in the pageContext (so, the fields of the beans and what they mean). Servlets are far better at generating the data that those JSPs will display and forwarding that data to the JSP. Used properly you get a seamless whole.
What you want is separation of data and presentation of that data, which is what the duality between JSPs and Servlets provides.
MVC has little to do with that as you can create each layer in whichever technology you like. The dual mechanisms of JSPs and Servlets just lend themselves naturally to different layers in an MVC architecture but that more or less grew over time rather than being a deliberate design decision. When first introduced, there was no JSP. Code was messy and each change in site layout required all the servlets being recompiled. Several initiatives came up with workarounds for this in the form of HTML template libraries and similar systems. In the end Sun prevailed with the JSP definition which in the beginning was rather ugly (it pays to be the guardian of the language) even though others were more elegant (for example Borland had a system back in 1999 which looked pretty much like JSTL does today). Over time the shortcomings of the original JSP system were addressed (again through multiple techs) which led to first the custom tag system which later evolved into the JSTL.