JSPs are text files filled with standard HTML code that is interspersed with nuggets of Java code. All this code, both the JSP and the nuggets of Java code, are interpreted by a JSP translator and turned into 100% Java code, along with any HTML that is found in the file. This architecture provides a script language (JSP) that integrates well with the Java VM, but that is simple enough to be learned by anyone capable of writing HTML well. The JSP page is translated into a servlet prior to being run. The servlet's outputs are HTML statements, so authoring JSP pages can be thought of as a fancy form of HTML authoring, with the additional option of adding programming logic. JavaServer Pages technology was created in response to complaints that creating HTML in servlets was too tedious and unnecessarily complicated. This complication kept Web site designers away from Java for many of their applications because they considered the learning curve associated with Java servlets to be too steep. One of the drawbacks of JSP technology is that JSP pages can become very difficult to debug and maintain if they contain too much Java. As a result, JSPs are heavily used in creating the View layer, but not often used for heavy Java logic. That kind of logic is better suited for normal Java classes called from the JSP or for Custom Tags, the implementation of which is done in Java classes. In either case, the JSPs provide View, and Java classes provide the Controller and the Model. Figure 26.6 shows this approach. Figure 26.6. JSPs are converted into servlets before they are run by a piece of software called the JSP translator. Notice that the JSP translator never interacts directly with the Web Server except to receive a request. The requested file is translated into a servlet before it can be run. The organization of the Model-View-Controller architecture is a little different with the JSPs because of the addition of Custom Tag Libraries to the picture. Figure 26.7 shows this new arrangement. Figure 26.7. By combining JSPs with regular Java classes and custom tags, we can implement a system that is faithful to the MVC architecture. Notice that there are no servlets in the picture. The servlets appear behind the scenes, but no servlet programming is required. The controller is composed of custom tags and ordinary Java classes. The Model layer is unchanged between the JSP approach and the servlet approach. |