JSP: A Beginner's Guide
Authors: Gary Bollinger, Bharathi Natarajan
Venue:  (2001)

From the Book:Introduction Are you a student trying to understand the fundamentals of modern Web development? Do you wonder how JavaServer Pages (JSP) work together with HTML, Java servlets, and databases to build scalable Web architectures? Have you been looking for a clear, structured introduction to JavaServer Pages? Do you develop Web sites and wish you could improve the performance of your CGI scripts? Have you ever wanted to use program loops or variables directly in your Web pages? Do you grow weary of writing obscure Perl programs that exert much of their effort printing out static HTML? Do you develop Java servlets to generate Web pages? Do you yearn for a more natural way to write dynamic pages? Do you seek a clean way to separate programming language constructs from HTML elements? Do you develop Active Server Pages (ASP) and hope to create Web sites on non-Microsoft platforms? Do you think there is a market for your skills on UNIX? Are you interested in design patterns and wonder how you should apply them to Web architectures? If you answered yes to any of these questions, this book is for you! The JavaServer Pages technology was developed by Sun Microsystems, Inc. Most obviously, the technology provides a better way to write dynamic Web pages. Web page development must often include both static HTML and dynamic content in its pages. Suppose, for example, you want to target the content of a page specifically for the person accessing the page. Most of the page provides the same look-and-feel for every person who accesses it. But some part of the page pulls content from a database tailored by user demographics or a pre-established login identity. Amazon.com, for example, provides a welcome message with a user's name and targeted advertisements. How might we imitate Amazon.com? Traditional CGI development uses a programming language, probably Perl or C/C++, to create a program that prints out large amounts of static HTML interleaved with responses from database queries. Even Java servlets (a companion Java technology) follow this basic paradigm; the only difference is that servlets live in a process thread, while Perl scripts execute as standalone processes. JavaServer Pages invert this approach, starting with static HTML and interleaving "tagged" Java code into the static HTML. The fact that the Java code is "tagged" is important-it means there is a clear separation between HTML and code. Consider the following JSP fragment (don't worry about the details right now): This JSP fragment consists of two Java code sections and an HTML section. The point to notice is that we insert both HTML and Java using tags. Of course, what goes between the Java tags is more complicated than what goes between the HTML tags, but we will show later on in Modules 5 and 6 how even Java scripting can be simplified using JSP. Now look at this servlet code fragment doing the same job performed by the JSP fragment: This servlet fragment buries static HTML in a Java println function call. The little piece of HTML shown here is comprehensible, but it is easy to see how confusing such statements can become. The problem lies with the inability of servlets to separate Java code from static HTML. It is the same problem when using Perl or C/C++ CGI scripts. JavaServer Pages solve this problem. Prerequisites We target this book at students and beginning Web and Java developers who seek a clear introduction to fundamental JSP concepts and techniques. While some of the material is difficult, we fill the book with examples, tips, and explanations that make it accessible to Web developers and programmers with modest experience. We address Java servlet programming in the context of JSP application architectures and in general provide what we believe is the necessary background information to use javaServer Pages effectively. Still, we assume and require some familiarity and experience with programming languages, preferably Java. We also assume some familiarity with the Internet. Structure of the Book We divide the book into three parts: Part I lays the technical foundations for building JavaServer Pages, focusing on syntax elements and their appropriate use. We divide Part I into six modules addressing the foundations of Internet technologies and each of the specific elements of JavaServer Pages. Part II introduces fundamental aspects of practical JSP development and focuses on the issues and techniques necessary to build "real-world" JSP applications. We divide Part II into four modules addressing common software patterns in Web architectures, Web security, application workflow, Ind integration of databases. By using such patterns, Web developers can create Web applications more quickly with fewer errors. Part III provides four appendices supplying ancillary information supporting our basic discussion. Part I-JSP Foundations Module 1 presents the broader world of Web technologies. It first describes the core architectural elements of Web application development, the Hypertext Transport Protocol (HTTP) and basic client-server issues. Then it provides an overview of the client and server technologies that extend the functionality of Web browsers and Web servers to support "real-world" applications. At the end of Module 1, you will understand the general universe of Web technologies. Module 2 narrows the focus to two JSP syntax elements, comments and directives, and their appropriate use in Web applications. Module 2 also describes the basics of a JSP page and lays the foundations for good programming style. At the end of Module 2, you will have built and displayed your first simple JSP page. Module 3 describes JSP scripting elements. We showed a few examples earlier in this Introduction. In Module 3 we describe scripting elements in detail and begin to build more complex JSP application elements. At the end of Module 3 you will be able to build complete JSP applications that exploit Java to script dynamic content. Module 4 describes implicit objects in JavaServer Pages. These objects provide a framework for passing and using information between the parts of a JSP application. At the end of Module 4 you will have built a JSP page that interactively responds to an HTML form. Module 5 describes standard JSP actions. JSP actions function as callouts (external references) to Java functionality. Among other things, standard actions provide a simple mechanism for incorporating helper JavaBeans in one's application. At the end of Module 5 you will be able to incorporate JavaBeans into your JSP application, thereby improving its style and professionalism. Module 6 presents custom JSP actions. Such actions provide one of the most powerful features of the JSP architecture. When writing custom JSP actions one is really extending the JSP engine itself. At the end of Module 6 you will have built your first such extension to the JSP engine. Part II-Building Real-World Applications Module 7 begins our discussion of some major aspects of "real-world" application development using JavaServer Pages. It introduces software patterns and their application to JSP architectures. We pay special attention to these patterns: Model-View-Controller (MVC) Mediator Command Strategy Bridge We also mention other patterns, but spend less time on them. These include factory, interceptor/filter, and callback. Module 8 addresses security issues. We describe some of the common security techniques used in Web architectures, including those required by the Servlet 2.2 specification. Module 9 describes issues surrounding application workflow-the control and movement from page to page in a JSP application. Module 10 addresses the role of databases in JSP architectures. We describe the JDBC data access API. Then we describe various techniques required to integrate a database into a JSP application. Part III-Appendices The appendices provide ancillary information supporting the rest of the book. Appendix A provides answers to special Mastery Check questions posed at the end of each module. Appendix B explains how to create and deploy a Web server and JSP environment. We concentrate on the Tomcat implementation of the Servlet and JSP containers and on the Apache Web Server. Appendix C describes how to set up and configure a MySQL database. This database engine is free and provides a fine platform for personal development projects. Finally, Appendix D describes the changes that occur between the current JSP specification JSP 1.1) and the proposed final daft of the JavaServer Pages 1.2 specification. Special Features Each module includes Hints, Tips, Cautions, and Notes to provide additional reference information wherever needed. Detailed code listings are included in gray boxes, many times with certain tags or features highlighted with further explanation. Most modules contain Ask the Expert question-and-answer sections to address potentially confusing issues. Each module contains step-by-step Projects to give you a chance to practice the concepts taught thus far. You can download the content for the projects from Osborne's Web site (www. osborne. com). Mastery Checks are included at the end of all modules to give you another chance to review the concepts taught in the module. The answers to the Mastery Checks are in Appendix A. Notational Conventions At several points, we present formats derived from formal specifications. When we do so, we use the following conventions: We display all literals in courier font: literal. When we treat collections of elements as a single unit, we display them in parentheses: (elementl element2). We indicate zero or more instances of an element using an asterisk: (elementl element2)*. We indicate an optional term between brackets: [element]. We indicate a carriage return character followed by a line feed character using the hyphenated sequence: CR-LF. We indicate symbolic elements such as variables or placeholder names using italics: element. We indicate environment variables in courier font, capitalized with `%' signs around the variable (Windows notation): %TOMCAT HOME%. We indicate directory names, relative paths, and file names in courier font using the backward-slash character for a file separator (Windows syntax): %TOMCAT HOME%\webapps\Book. We indicate Java Class names, interface names, and method names using italics: HelperBean.getProperty. Throughout the development of this book, our objective has always been to provide you with a cohesive, easy-to-understand guide for coding JavaServer Pages that helps you get up and running in no time. We applaud your decision to learn JavaServer Pages and encourage you to use the Internet to its fullest potential, both during the learning process and in your ensuing Web application development. Have fun and good luck! Other Readings 1. Alexander, C., S. Ishikawa, and M. Silverstein. A Pattern Language. Oxford University Press, 1977. 2. Alexander, C. The Timeless Way of Building. Oxford University Press, 1979. 3. Alur, Deepak, John Crupi, and Dan Malks. Core J2EE Patterns: Best Practices and Design Strategies. Sun Microsystems Press, Prentice Hall, 2001. 4. Berners-Lee, Tim, with Mark Fischetti. Weaving the Web: The Original Design and Ultimate Destiny of the World Wide Web by Its Inventor. Harper San Francisco, 1999. 5. [DBTags] http://jakarta.apache.org/taglibs/doc/dbtags-doc/l*ntro.html 6. Gamma, Erich, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Publishing Company, 1995. 7. Geary, David. "JSP Templates." Java World, September, 2000. http://www.javaworld.com/javaworld/jw-09-2000/jw-0915-jspweb_p.html 8. Lea, Doug. "Christopher Alexander: An Introduction for Object-Oriented Designers." SUNY Oswego/NY CASE Center, December, 1993. http://g.oswego.edu/dl/ca/ca/ca.html 9. [MySQL] http://www.mysql.com 10. [Struts] http://jakarta.apache.org/struts/index.html 11. [Taglibs] http://jakarta.apache.org/taglibs/index.html 12. [Tomcat] http://jakarta.apache.org/tomcat/index.html