Java Servlet and JSP Cookbook

In the great tradition of cookbooks, O’Reilly has published the Java Servlet and JSP Cookbook. This book, written by Bruce W. Perry is a must-own book for anyone working with web applications in the Java space. I’ve been a Java developer for almost 8 years now and have been working with Servlets since early 1999 and I’ve learned quite a few things from this book.

Java Servlet & JSP Cookbook
By Bruce W. Perry
Paperback: 704 pages
Publisher: O’Reilly & Associates; 1st Edition edition (December 1, 2003)
ISBN: 0596005725

The Java Servlet & JSP Cookbook provides more than 200 ‘recipes’ or fully working and documented code snippets that you can directly cut-and-paste in your application. The book starts off with a quick intro to writing servlets and JSP pages. I was very impressed that the first JSP page that you write uses JSTL and is not loaded up with scriptlet code. I am just sick and tired of arguing with people with scriptlets are bad and it’s nice to see a book that starts off with JSTL. Kudos Bruce.

Once the intro is complete, you move onto writing deployment descriptors, deployment and then move on to Ant. One of the most common question people ask after deploying JSP based application is the idea of precompiling JSPs for performance reasons. The fifth chapter does a great job of suggesting several methods of precompiling JSPs. I should also mention that the book includes how-to guides for Tomcat and WebLogic, which covers a pretty large landscape of web containers. WebSphere, Resin, Jetty are not directly covered but who uses anything but WebLogic, right? 🙂

The book then moves on and covers topics such as handling Form data via POST/GET, uploading files, cookies, session tracking and URL rewriting. There is also a chapter on JavaScript and how they use JavaScript with servlets. I don’t really understand the point of this chapter as most users just need a few cut-n-paste JavaScript for client-side FORM validation. There is also a chapter on streaming non-HTML content such as PDF, audio/video files and others to the browser.

I also liked the chapter of logging in Servlets and JSPs. This chapter includes a nice introduction to Log4j and a nice tag library that uses Log4j under the cover. My favorite chapter in this book was the chapter dealing with authentication. The chapter starts off by talking setting users in Tomcat and then moves into setting up BASIC authentication. The next recipe talks about using Form-based authentication. The chapter is rounded off with a good treatment of the Java Authentication and Authorization service (JAAS). In this chapter, you create your own custom LoginModule and then use JAAS in a servlet and JSP.

There is also a chapter about embedding multimedia content inside JSPs. This is not something I’m really interested in and I just glossed over this chapter. The same goes for the next chapter on manipulation of the HttpRequest. The next chapter does a great job of exploring Servlet Filters, which is a great feature introduced in the Servlet 2.3 specification that hasn’t really caught on. Filters are great and the book includes some great examples of how best to use them.

The next section includes chapters on sending, accessing email from servlets along with database access. Most complex application usually will implement some backend service to access database and separate the business logic from the data and the data from the UI, but the included recipes will help get you up and running for simple application.

I really liked the section on custom tag libraries and JSTL. Tag Libraries are a great way to avoid scriptlet code in JSPs. The chapter on JSTL is also fairly comprehensive and includes code snippets for the core, XML, format, and SQL tags. There is also a great section on the Expression Language (EL) which has been migrated from JSTL 1.0 to the JSP 2.0 specification.

I could go on about this book but I won’t bore you any longer, assuming you are still reading. I highly recommend this book for anyone doing any type of Web development using Servlet and JSPs. I mentioned this earlier, but I’ve been writing Servlets and JSPs for the past 5 years and I’ve learned quite a few things from this book. Add this book to your library today. The code for this book is available on O’