Core JSP

From the Book: PREFACE: FOREWORD In recent years, a large amount of software development activity has migrated from the client to the server. The client-centric model, in which a client executes complex programs to visualize and manipulate data, is no longer considered appropriate for the majority of enterprise applications. The principal reason is deployment—it is a significant hassle to deploy client programs onto a large number of desktops, and to redeploy them whenever the application changes. Instead, applications are redesigned to use a web browser as a "terminal." The application itself resides on the server, formatting data for the user as web pages and processing the responses that the user fills into web forms. If you set out to develop a web application, you need to choose a technology that has several important characteristics. You need to generate large amounts of dynamic HTML conveniently. You require access to databases and other services. The technology must provide an architectural foundation for performance and stability. Finally, you must be able to partition your pro-gram logic in a way that allows for future growth and maintainability. The first web applications used the CGI (Common Gateway Interface) mechanism and a collection of server-side scripts, typically written in Perl, but occasionally in C, Python, PHP or other languages. There are numerous problems with this approach. The CGI mechanism does not scale well since every web request spawns a new server process. Communication between processes-for example, to share resources such as database connections-is extremely awkward to program. And finally, exotic programming languages may havetheircharm but they lack the ability to do the "heavy lifting." Features such as database access or security are typically not part of the language but supplied by a non-standard third-party library. That puts the programmer at the mercy of not only the implementors of the language itself but also the providers of various third-party libraries. Java programmers have enjoyed the power of servlets for some time, which solves many of these problems. Servlets are programmed in Java, a language that is widely supported. Java has built-in features for database access, networking, multithreading, security, and so on. Each servlet executes in its own thread, thus avoiding the cost of generating server processes. Servlets can easily share resources such as session state and database connections. The principal disadvantage of servlets is that it is plainly tedious to generate HTML. All HTML must be generated programmatically, by statements that print all the text and tags. In particular, that means that the pages are generated by programmers. We all know what can happen when programmers try their hand at web design. An increasingly popular approach in recent years has been the use of webserver scripting languages such as Netscape LiveWire and Microsoft ASP (Active Server Pages). With these systems, a programmer embeds code snippets into web pages. The pages themselves can be professionally designed by a web designer. The web server executes the code snippets when serving the page, inserting the HTML that results from the execution of each snippet. The advantage of this approach-and the reason for its popularity-is that you can get simple results very quickly. But what looks like fun and great productivity early on turns out to be a maintenance nightmare later. When you intermingle the presentation (the static parts of the HTML pages) and the business logic (the code snippets), it becomes very difficult to change either when the need arises. Web designers will not know how to move the code around when redesigning the pages. This makes any redesign a costly affair involving frequent interaction between programmers and web designers. Finally, keep in mind that you are tied into a particular web server. For example, if you develop your application in ASP and later want to use Apache instead of Microsoft IIS, you are stuck. The JSP technology that is the topic of this book overcomes these issues. JSP has the same advantages as servlets—in fact, JSP pages are servlets. You use the full power of the Java language, and not some scripting language, to implement your business logic. By using beans, XML transformations, and tag libraries, JSP lets you separate the presentation logic and business logic. For example, in a well-structured JSP application, you can have the same business logic with multiple interfaces, giving your users the choice to use a regular web browsers or a mobile phones that uses WAP (the wireless access protocol). This book teaches you how to build robust and scalable web applications with JSP. It covers the JSP syntax, the features that JSP inherits from servlets such as session management, the interaction between servlets and beans, a number of useful Java topics such as JDBC (Java Database Connectivity) and XML. Finally, and most importantly, you will learn about application partitioning and deployment-these subjects make all the difference between a quick hack and a robust application that will withstand the test of time. Unlike other books, this book takes a properly JSP-centric approach, in accordance with the recommendations that Sun Microsystems makes in their Java Enterprise blueprints. This is very appropriate and a major strength. Where other books start out with servlets and discuss JSP as a second method for web programming, this book shows you why JSP pages have a higher position in the food chain. A JSP page can do everything a servlet can, but where you have to do a lot of tedious programming and organizing when you use servlets, JSP has higher level capabilities that let you focus on your business problems instead. In the spirit of the Core series, this book contains is plenty of real-world advice that you won't find in the online documentation. The authors don't dwell on tedious syntax and boring minutiae. Unlike so many computer book authors, they have done the hard work and separated the wheat from the chaff. You won't waste time studying features that you won't use, but you will find good coverage of those subjects that you actually need when building real applications. I am confident you will find this Core book truly useful. I hope you enjoy it and have the opportunity to use it for building great web applications. Cay Horstmann San Jose, August 2000