4

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 4

3

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
  • 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.
    – Matt
    Oct 22, 2012 at 12:30
2

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.

1

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
  • 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. 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. Oct 22, 2012 at 14:32
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.