Why Choose JSP Over Servlets for View in Java EE?

java-eejsp

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.

Best Answer

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).

Related Topic