wood burning stoves
The moose likes JSP and the fly likes Why JSP over Servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Why JSP over Servlets?" Watch "Why JSP over Servlets?" New topic

Why JSP over Servlets?

prabhu peruka

Joined: Oct 31, 2004
Posts: 5

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.

Thanks in advance
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Maintaining a complex HTML page generated in StringBuffers in Java code is nothing short of a complete nightmare.

[Asking smart questions] [About Bear] [Books by Bear]
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
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.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.

I agree. Here's the link: http://aspose.com/file-tools
subject: Why JSP over Servlets?
It's not a secret anymore!