Chapter 14. Combining Servlets and JSP

As I described in the previous chapter, combining servlets and JSP pages lets you clearly separate the application logic from the presentation of the application; in other words, it lets you use the most appropriate component type for the roles of Model, View, and Controller. To illustrate how a servlet can act as the Controller for an application—using beans as the Model and JSP pages as Views—we redesign the Project Billboard application from Chapter 10, in this chapter. Along the way, we look at how servlets and JSP pages can share data, how to deal with URL references between servlets and JSP pages in a flexible manner, and how to handle runtime errors consistently in an application that mixes these two technologies.

Java servlets offer a powerful API that provides access to all the information about the request, the session, and the application data maintained as servlet context attributes. Chapter 2, contains a very brief introduction to the servlet API, and Appendix B, contains reference material for the main classes and interfaces. To really make use of the techniques described in this chapter, however, you need to know more. If you haven’t worked with servlets, I recommend that you read up on them (try Jason Hunter and William Crawford’s Java Servlet Programming from O’Reilly) before you apply the ideas presented here in your own application.

Using a Servlet as the Controller

In an application in which all requests must be preprocessed ...

Get Java Server Pages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.