O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Enhancing the servlet to “call” the JSP (version three)

In this step we’re going to modify the servlet to “call” the JSP to produce the output (view). The Container provides a mechanism called “request dispatching” that allows one Container-managed component to call another, and that’s what we’ll use—the servlet will get the info from the model, save it in the request object, then dispatch the request to the JSP.

The important changes we must make to the servlet:

1 - Add the model component’s answer to the request object, so that the JSP can access it. (Step 4)

2 - Ask the Container to forward the request to “result.jsp”. (Step 5)

image with no caption

1 - The browser sends the request data to the container.

2 - The Container finds the correct servlet based on the URL, and passes the request to the servlet.

3 - The servlet calls the BeerExpert for help.

4 - The expert class returns an answer, which the servlet adds to the request object.

5 - The servlet dispatches to the JSP.

6 - The JSP gets the answer from the request object.

7 - The JSP generates a page for the Container.

8 - The Container returns the page to the happy user.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required