Reading Notes for Chapter 2,
Head First Servlets and JSP

P. Conrad, CISC474, Spring 2006

Table of Contents

p37Web App Architecture p47Mapping servlet names improves your app's flexibility and security p57Is there an answer?
p38Objectives p48Using the Deployment Descriptor to map URLs to servlets p58Sharpen your pencil (MVC)
p39What is a Container? p49But wait! There's more you can do with the DD p59Sharpen your pencil (Who's responsible?)
p40What if you had Java, but no servlets or Containers? p50Story: Bob Builds a Matchmaking Site p60Code Magnets
p41What does the Container give you? p51He starts to build a bunch of servlets... one for each page p61Code Magnets (continued from p. 60)
p42How the Container handles a request p52But then it gets ugly so he adds JSPs p62Solution to p.59
p43(continued from p.42) p53But then his friend says, "You ARE using MVC, right?" p63Solution to pp.60-61
p44How it looks in code (what makes a servlet a servlet) p54The Model-View-Controller (MVC) Design Pattern fixes this p64A "working" Deployment Descriptor
p45You're wondering how the Container found the Servlet p55Applying the MVC pattern to the matchmaking web app p65How J2EE fits into all this
p46A servlet can have THREE names p56But then his friend Kim takes a look   

p37 Web App Architecture

This page highlights the main things covered in Chapter 2: the servlet lifecycle, the role of the Container, and the Model-View-Controller design pattern.

Some background on "design patterns"

If you haven't encountered "design patterns" before, the following quotes from the book J2EE Design Patterns (cited below) may help set the right context for "design patterns" as we will use them in this course:

A design pattern is a recurring solution to a recurring problem....Good patterns strike a balance between the size of the problem they solve and the specificity with which they address the problem.

The simplest patterns may be summed up in no more than a sentence or two. Using a database to store information for a web site is a pattern, albeit a fairly high-level and obvious one. More complex patterns require more explanation...

We hope that readers will have two reactions as they explore the patterns in this book. The first is to say, "That's cool" (or, if the boss is around, "That's useful!"). The second is to say, "So, what's the big deal?" Both reactions mean that we're doing our job—describing approaches that work. Design patterns in enterprise applications can be surprisingly simple on the surface. Of course, it's that generic aspect that makes them effective patterns in the first place.

I would also recommend looking over the Wikipedia entry on "design pattern (computer science)".

Further reading on "design patterns" (optional)

Free Online Resources

Books

 

p38 Objectives

As in Chapter 1, the objectives covered in Chapter 2 are also covered in more detail in later chapters. Nevertheless, look this page over before you read Chapter 2. After you read Chapter 2 to see how many of these SCWCDE exam objectives you've managed to achieve.

p39 What is a Container?

We'll be working with Tomcat all semester, and perhaps with Resin as well. So it is important to understand what Tomcat and Resin are. They are both examples of "Servlet Containers".

Furthermore, Servlets can only exist inside a "Servlet Container". The container gives birth to servlets, supports them throughout their little lives, and takes care of them as they die.

Pages 39-45 cover the basics of what a Servlet Container is, and how a servlet interacts with the Container. Read carefully, because this material will likely lead to some CISC474 midterm or final exam questions. If you don't believe me, look at question 3 on the first midterm exam from Spring 2005.

Understanding about containers is also fundamental to understanding how to work with web applications composed of Servlets (and later, also, JSPs).

p40 What if you had Java, but no servlets or Containers?

This page asks you to speculate about what kinds of thing the Container must be doing for a Servlet. Skim through Chapter 1 again, and think about what happens when you run the example servlet from Chapter 1. The idea is to think about everything that is going on that is NOT in the code you wrote for your servlet; those are all the things that the Container must be doing for you.

It will really help your learning if you actually make the list and don't just cheat by looking at the answers at the bottom of the page. That "interactive reading style" is what the Head-First approach to textbooks is all about (read over the preface on pages xxi through xxvii if you haven't done so yet; read about the Tiger and what it has to do with learning about Servlets).

p41 What does the Container give you?

This page is an important list, so spend some time on it. These are all the things you don't have to worry so much about because of the help the Container gives you.

p42 How the Container handles a request
p43 (continued from p.42)

Pay particular attention to when the request and response objects get created, and when the thread starts running and when it stops running. Also note when the doGet() or doPost() methods get called. When you write code for webapps, these are all things you have to keep in mind if you want your code to work.

p44 How it looks in code (what makes a servlet a servlet)

Something your book doesn't point out until later: HttpServlet is a subclass of GenericServlet.

The book notes that 99.9999% of all servlets are HttpServlets. Without citing (probably made up) statistics, Sebesta agrees: "Most user-written servlets are extensions to HttpServlet" (p. 431), though in a footnote points out that there are other kinds of servlets, for example there is a class SOAPServlet for SOAP (Simple Object Access Protocol, the basis of so-called "Web Services"). SOAPServlet is also a subclass of GenericServlet.

However, in CISC474, we'll mainly (or perhaps even exclusively) focus on HttpServlets.

The Q and A at the bottom of this page will help you understand why you need fourteen chapters to get through an explanation of Java server-side web development. The diagram on pp. 42-43 and the code on page 44 raises two important questions, and the full answers don't come until later in the book.

p45 You're wondering how the Container found the Servlet

Again, you should try to speculate about the answer before you turn the page; you are being asked on this page to answer a question that you have not yet been given the answer to. That's what the "Flex Your Mind" activities are all about. You should feel a bit of panic as you have to grasp at straws a bit and really think.

That gets your brain juices flowing so that when you turn the page, what you read is more liable to stick to your brain (all those brain juices make your brain stickier.)

p46 A servlet can have THREE names

An important page: if you don't believe me, look at questions 7 and 8 from the Spring 2005 first midterm exam.

The explanation of this concept continues onto pages 47 through 49, so read all of these pages together.

p47 Mapping servlet names improves your app's flexibility and security

The character on this page (the woman with her hands on her hips) is one of my favorite characters in the Head-First books. Unfortunately, she doesn't have a name; "Bob" and "Kim" get names, but this lady is without moniker.

What I like is that she introduces some drama into what could be otherwise very dry material. Whenever she shows up, be sure to read her "thought bubble" (usually brimming with attitude and sarcasm) and the response. Imagine whether you think she would be satisfied with the answer, or just roll her eyes and say "what-ever!"

As an exercise, also come up with your own name for her.

On a serious note, the content on this page goes along with that on page 46, and as you already know, the content on p. 46 was the subject of a CISC474 exam question in the past. And she's one of my favorite characters in the book. Do I need to spell it out for you?

p48 Using the Deployment Descriptor to map URLs to servlets

Check the confirmed errata for this page

Compare the code on this page with the web.xml file from p. 30. Can you see how the material on p. 48 explains the contents of the web.xml file on p. 30?

p49 But wait! There's more you can do with the DD

Be sure you read everything on this page, including

Remember that each page contains a lot of content; the way it is laid out may cause you skim over some parts.

Also note the "foreshadowing" in the Q & A of more stuff to come in Chapter 11 ("Deploying your Web App"). Hopefully this builds some suspense and makes you want to read on (just eight and a half chapters to get through first!?)

p50 Story: Bob Builds a Matchmaking Site

This page starts a story about Bob and Kim that continues through p. 57 of this chapter.

The story sets the stage for you to ask a lot of questions on p. 57. The answers to these questions don't come until Chapter 14, almost at the very end of the book (pages 730-745), and the answers have something to do with the web development framework called "Struts".

I'm told by students who have graduated and are in the so-called "real world" building "real web apps" and making "real money", that using "Struts" is very cool. It helps you be a lot more productive with less work, and hopefully be the go-to-gal or go-to-guy; the one that keeps his/her job when others are laid off. Knowing Struts can also get you a job at another company with a promotion and a pay raise when you get sick of your boss.

So, if you hope to be able later to understand "Struts" and why it is a cool thing, it may help to spend some time on this story now.

p51 He starts to build a bunch of servlets... one for each page

Look over Bob's design. Also look at his template code. Does it make sense to you? Try to understand it completely before you turn the page, and also see if you can find anything about it that you don't like.

p52 But then it gets ugly so he adds JSPs

Same with this page. Understand it completely before going on to page 53.

Note one thing we haven't covered yet is how step "5" occurs—that is, if you want to:

then there has to be some handoff in the middle (step 5.) For now, don't let that detail bother you; assume that it can be done in a straightforward way. (The text covers the details of that in Chapter 3, pages 88 and 89, but you don't need to look over those now; it will just interrupt the flow of the story.)

p53 But then his friend says, "You ARE using MVC, right?"

This page puts the whole MVC thing in a context by adding some "human drama", i.e. the conversation between Bob and Kim.

If you are familiar and comfortable with MVC, this will immediately make sense to you, and you'll know exactly what Kim is talking about.

If you are new to MVC (or rusty) read this over, and then after you've read through pages 54-57, and later Chapter 3 (Mini-MVC-Tutorial), come back and read this page again. Kim's comments will probably make a lot more sense then.

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

This page introduces Model-View-Controller. Get the big picture from this page, but if it is still murky, don't worry; Chapter 3 is an entire chapter called "Mini-MVC-Tutorial".

Know that this is an important concept though: see question 9 on the Spring 2005 first midterm exam.

MVC is so important, that some geeks have even been moved to song: lyrics mp3 (5:46)

p55 Applying the MVC pattern to the matchmaking web app

So, is this what MVC is all about? What do you think of this design?

On the next page, Kim is going to take a look (hint: Kim is not going to like it.) What do you think Kim will criticize?

p56 But then his friend Kim takes a look
p57 Is there an answer?

This is the biggest Flex Your Mind question in the whole book, in some sense, because the answer doesn't come until almost the very last page.

The first time you encounter this Flex Your Mind question, you probably aren't really ready to think in too much detail about it, because unless you've written MVC based servlets before, you don't have a really good idea about what "duplicate code" Bob and Kim are talking about. So my advice is, think about this for a few minutes now, but not too long.

Then:

p58 Sharpen your pencil (MVC)
p59 Sharpen your pencil (Who's responsible?)

Check the confirmed errata for these pages!

Again, good practice for exam questions.

p60 Code Magnets
p61 Code Magnets (continued from p. 60)

I put several hours of work into programming these "code magnets" in JavaScript so that you could actually solve the puzzle the way it was intended to be solved. So go to the link readingNotes/HFSJ/ch02/ch2.codeMagnets.p60.p61.html and give it a try. Really try to solve the puzzle before you click the "solve" button or look at the solution on p. 63.

Side comments on my Code Magnet application

I tested this in Firefox 1.5 and IE6. It works a little better in Firefox than in IE6, but it should work ok in either one. The code is based on a drag and drop JavaScript library from Walter Zorn. He has several nice JavaScript libraries.

A good JavaScript project, if anyone wants to take it on, would be to add a functionality to be able to do multiple selection and grouping (so that you can move a whole bunch of magnets at once, the way you do in more drawing programs.) You can probably do this through Walter Zorn's API, without having to modify his source code. Some other nice add-ons would be

p62 Solution to p.59

Check the confirmed errata for this page!

Don't peek until you've tried to solve the problem yourself!

p63 Solution to pp.60-61

The "solve" button on my code magnet app should give you this solution.

p64 A "working" Deployment Descriptor

Here I agree with the book; it is NOT important to memorize or expect to be asked to reproduce the yada-yada-yada in the <web-app> open tag (or to be more precise, the "values of the attributes of the web-app element"). Typically, you'll be creating a Deployment Descriptor by copying an existing one as a template, so you'll get all that for free anyway.

However, if you are the type or person that wants to understand the details, you can learn a bit more about what all this means by reading up on XML namespaces in Sebesta.

(Of course, you probably will need to read Sections 8.1 through 8.4 first, if you haven't already. Better, yet, just take this on faith for now, and don't worry about it for now. At some point, we may cover XML namespaces and/or XML schema in a lecture or an assignment, but unless and until we do, I won't ask about the details of this on an exam.)

p65 How J2EE fits into all this

Check the confirmed errata for this page!

Understanding the relationship between J2EE , EJB, the Servlet Spec, JNDI and JMS... this may seem like so much alphabet soup, but it is the kind of stuff that tends to come up during job interviews.

So, spend some time trying to understand these relationships, even if it seems like something that you won't really need to complete the programming assignments in this course. (To be honest, it isn't.)


End of CISC474 reading notes for HFSJ, Chapter 2


Valid XHTML 1.1 Valid CSS!