The Model-View-Controller (MVC) Design Pattern fixes this

If Bob had understood the MVC design pattern, he would have known that the business logic shouldn’t be stuffed inside a servlet. He would have realized that with the business logic embedded in a servlet, he’d be screwed if he one day needed a different way to access the dating service. Like from a Swing GUI app. We’ll talk a lot more about MVC (and other patterns) later in the book, but you need a quick understanding now because the tutorial app we build at the end of this chapter uses MVC.

If you’re already familiar with it, then you know that MVC is not specific to servlets and JSPs—the clean separation of business logic and presentation is just as valid in any other kind of application. But with web apps, it’s really important, because you should never assume that your business logic will be accessed only from the web! We’re sure you’ve worked in this business long enough to know the only guarantee in software development: the spec always changes.

Model*View*Controller (MVC) takes the business logic out of the servlet, and puts it in a “Model”—a reusable plain old Java class. The Model is a combination of the business data (like the state of a Shopping Cart) and the methods (rules) that operate on that data.

MVC in the Servlet & JSP world

image with no caption

Get Head First Servlets and JSP, 2nd Edition 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.