February 27, 2021
Hot Topics:

Understanding Servlets

  • By Steven Haines and Steven Potts
  • Send Email »
  • More Articles »


This chapter has introduced you to the world of servlet development. We started with a discussion of how servlets worked. Following that, you learned how to set up your development environment.

You learned how the HTTP protocol, the language of servlets, works. Following that, you learned how to create servlets using both the GenericServlet class and the HttpServlet class.

We covered how to create servlets that called other classes and how to deploy those classes. Then you learned how to preserve servlet state in the browser using cookies. Finally, you learned how to use the HTTPSession interface to store data on the server between servlet calls by the same browser.

Review Questions

  1. What is the difference between a servlet and a Java application running on the server?

  2. What is servlet container?

  3. What is the difference between the HTTPServlet class and the GenericServlet class?

  4. What is a session object?

  5. What is the difference between a cookie and a session object?


  1. Create a servlet that echoes your name when called.

  2. Create a servlet that lets you choose toppings on a pizza.

  3. Expand the pizza servlet to use cookies to remember your last order and ask you if you want the same toppings this time.

  4. Modify the pizza servlet to use a session to accomplish this.

  5. Move the creation of the HTML to be returned by the pizza servlet to a class in a package.

About the Authors

Steve Haines has worked in the enterprise software industry for the past eight years and has been focusing on Java since 1997. He has been filling key architectural roles in the areas of B2B e-commerce, high-speed Internet marketing, application monitoring and diagnosis, and robust client and server-side image layout and management over the past few years. He is currently the J2EE Domain Architect for Quest Software and is responsible for defining the expert rules for tuning and monitoring Enterprise Java applications and application servers.

He is the author of Que Publishing's Java 2 from Scratch and has numerous articles on InformIT.com in the areas of Java Swing and Enterprise Java. He shares author credits on Java Web Services Unleashed, C++ Unleashed, Sams Teach Yourself C++ in 21 Days, and Sams Teach Yourself Java in 21 Days. He has also worked as a technical editor for Pearson Education in areas of Java, Enterprise Java, Network Communications, C++, and video-game programming. Steve has taught all aspects of Java programming from basic certification training through Database, Web Development, and Enterprise JavaBeans at Learning Tree University (LTU). Steve recently enrolled in a Bachelor's of Biblical Studies at Calvary Chapel Bible College.

Steve Potts is an independent consultant, author, and Java instructor in Atlanta, Georgia. Steve received his Computer Science degree in 1982 from Georgia Tech. He has worked in a number of disciplines during his 20-year career, with manufacturing being his deepest experience. Steve has consulted for such companies as Home Depot, Disney, and IBM. His previous books include Java Unleashed and Java 1.2 How-To. He can be reached via email at stevepotts@mindspring.com.

Source of this material

This is Chapter 21: Servlets from the book Java 2 Primer Plus (ISBN: 0-672-32415-6) written by Steven Haines, published by Sams Publishing.

To access the full Table of Contents for the book.

Other Chapters from Sams Publishing:

Web Services and Flows (WSFL)
Overview of JXTA
Introduction to EJBs
Processing Speech with Java
The Java Database Control in BEA Weblogic
Databases and Tomcat
Working with JAX-RPC

Page 9 of 9

This article was originally published on April 8, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date