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.
4 Answers
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()
callsusually 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).
-
There is a cost the first time a JSP is accessed, because the application server must convert the JSP to a servlet. This can be avoided if JSPs are precompiled to servlets and deployed with the WAR, as part of the build process.– MattCommented Oct 22, 2012 at 12:30
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.
-
JSP and servlets are so tightly coupled that the abstraction you mention isn't really useful, e.g. in JSP you can access Java variables which will be used in the generated servlet. Any custom JSP interpreter that covers these as well would therefore be one kind of reimplementation of servlets or another. Commented Oct 22, 2012 at 11:49
-
I know that they're tightly coupled, but the compilation into a servlet really shouldn't be a thought when writing a JSP. Commented Oct 22, 2012 at 14:32
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.