Although servlets does the task of sending the HTML code to the client programmers weight JSP over servlets for that. Even a JSP code is compiled into servlet before giving the response to the browser then why is JSP preferred? If servlet is used then a lot of computation work can be saved.
why do programmers weight JSP over servlets for that?
You can look at JSP as just another more friendly syntax for writing servlets. So, it’s not so much a choice between JSP and servlets: the choice is between writing servlets in JSP syntax and writing them in raw Java. As to why would someone prefer the first over the latter, for many (not all) situations raw Java syntax is very inconvenient since:
-
entire page is inside the
out.println()
calls -
usually means lots of markup within strings
-
for non trivial pages this is insanely difficult to read not to mention debug
-
the increased development cost is much much greater than the lowered performance cost (which isn’t lowered at all, see the next point)
If servlet is used then a lot of computation work can be saved.
No, not really. Well, only computation at deploy time, when servlets are generated from the JSPs, but during run time it’s the same (assuming you disable the option to generate servlets in every request).
1
In General, it would be harder to code complex Java code and put it inside a JSP, next is, It would be a really really messy code if you are going to combine complex java code inside a JSP.
Just my two cents.
Just because a JSP is compiled into a servlet doesn’t mean there is no difference between these two. Actually, there might be someone out there who wrote a JSP interpreter that doesn’t translate a JSP into a servlet but performs any other kind of execution.
Think about the concept of servlets and JSP and compare these concepts against each other – not the actual implementations.
2
JSPs are preferred if you are mainly augmenting an HTML template with server-generated data (e.g., generating a table of information or filtering a collection of images/documents). Servlets are preferred for functions that don’t have UI-related work (e.g., filters and web services). JSPs seem to becoming a little less common nowadays, with more work being done with straight web pages making AJAX calls to web services, but they can still be useful.