While Servlets provided a Java-based solution for generating dynamic webpages, many find Servlets cumbersome to develop. Needing to write line-after-line of out.println() is both cumbersome and it obscures the resulting HTML.
In apparent admiration of Microsoft's cleaner Active Server Pages (ASP) solution for dynamic webpages, Sun introduced JavaServer Pages (JSP) in 1999. JSP lets the developer mix static HTML with dynamically generated HTML (JSP directives produce dynamic content). The following sample JSP code is equivalent to the longer Servlet code sample:
<html> <body> <%@ page language=java import= %> <H1> Welcome back, <% String user = (String) session.getAttribute(user); out.println(user); %> </H1> </body> </html>
As you can see in the code sample, JSP dynamic directives are surrounded by <% %> while the standard HTML is simply written "as is".
In fact, JSPs are simply built on top of Servlets. The Web Server translates the JSP into a Servlet (with the more cumbersome out.println syntax, etc.) The beauty is that the developer only needs to put together the simpler JSP page and let the web server do the translation to Servlet code.
How important are JSPs since they essentially fill the same application space as Servlets? Sun has stated that "JSPs are the norm" while Servlets will be the exception in terms of developing dynamic webpages. In other words, Servlets would only be used for creating dynamic webpages in those unusual situations where JSPs cannot effectively do the job. Given that vendors have quickly added JSP support to their web servers, the industry seems to support Sun's direction.
MVC Model: Combining JSP and Servlets
A common model now used in the J2EE Presentation Tiers is called the Model View Controller (MVC) model. With MVC, requests are sent to a central Servlet "dispatcher". The Servlet dispatcher then transfers control to the appropriate JSP to produce the dynamic webpage. The MVC model has the benefit that flow-control between webpages is located in a centralized location (a Servlet) while the easier-to-use JSP technology is used for producing the actual dynamic webpages.
... back to Java Topics