Let me make a general remark up front: in my view the main difference between JSPs and Servlets is that of emphasis. A JSP is a presentation-centered servlet. A servlet is a processing-centered JSP
. The appropriate choice is dictated by the nature of the task at hand: presentation or processing.
Originally posted by Bharatesh H Kakamari:
There are quite many advantages of JSP over servlets.
1. Jsps have the ability of dynamic html generation.
So have servlets, even if out.println doesn't offer much in the way of sugar coating.
2. As far as the performance is concerned both have the same performance. Even though when a jsp page is accessed for the first time, it takes some time for the jsp engine to compile this page to a servlet and then execute this servlet. THIS DISADVANTAGE HAS BEEN REMOVED BY PRECOMPILATION OF JSP PAGES SO THAT EVEN IF THE JSP PAGE IS ACCESSED FIRST TIME THE USER ALWAYS GETS A PRE-COMPILED PAGE.
But the request is always routed through the JSP container servlet which usually checks the timestamp and then forwards to the actual jsp servlet, thereby imposing a (usually insignificant) bit of overhead.
3. The best advantage of Jsp over servlets is JSP technology separates content logic from business logic. The role of context developer is separated from component (business logic) developer. A web designer need not know java language to code servlets logic.
A good developer will be as careful separating presentation from business logic in Servlets as in jsps. In fact my experience is that jsps aren't any better in encouraging good separation of responsibilities than servlets are! There's too much Java code in too many jsps and in the worst cases it deteriorates into a more unsightly mess than you can ever achieve with servlets.
Having a good architecture is still too much down to disciplined development.
- Peter