Home > Articles > Programming > Java

Introduction to Servlets

  • Print
  • + Share This
Servlets are a fundamental part of JSPs. Learn what a Servlet is, the JSP/Servlet lifecycle, and the translation of JSPs into their Servlet equivalent code.
This sample chapter is from Sams Teach Yourself JavaServer Pages in 24 Hours, by Stephanie Fesler.

Hour 3: Introduction to Servlets

Talking about Servlets may seem out of place to you in a book about JSPs. However, the underlying implementation of JSPs is via Servlets. That is right: JSPs get translated into Servlets before they are executed. You may be wondering how this impacts performance. It may hurt performance on the first hit, but you are going to see ways to avoid this performance decrease on subsequent hits. Throughout this book you will learn new JSP tags and you will also see how those tags get translated into the equivalent Servlet code. Understanding the translation into Servlet code will help deepen your understanding of the behind-the-scenes activities of JSPs.

In this hour you are going to learn

  • What a Servlet is

  • The JSP/Servlet lifecycle

  • The translation of JSPs into their Servlet equivalent code

What Is a Servlet?

A Servlet, in the most general case, is a Java class that implements the Servlet interface and accepts requests and generates responses. The requests can come from Java classes, Web clients, or other Servlets.

When you implement an interface you are saying that your class provides implementations for the methods declared in the interface. Therefore, when you implement the Servlet interface you are declaring that your code will provide implementations for the methods in the Servlet interface.

For example, if you are writing a banking example and you have many classes that need to provide a definition for methods withdraw() and deposit(), you can write an interface that declares these methods. The other classes would implement the banking interface and will guarantee that they provide definitions for the behavior of the methods withdraw() and deposit().

Since this book concentrates on JSPs and the Web, the focus on Servlets will be on a specific type of Servlet, the HttpServlet. The HttpServlet accepts HTTP requests and generates HTTP responses. When you write your HttpServlet, you do not implement the Servlet interface directly; instead you extend the HttpServlet class.

NOTE

Extending a Java class creates a class hierarchy, much like a family tree. The class that is being extended from is called a superclass, or parent class. The class that declares it is extending another class is called the subclass, or child class.

You are an extension from your parents and you inherit certain attributes and behaviors from them. The same is true for Java classes. The child class inherits the attributes and behaviors of the parent class, but has other attributes and behaviors that make it unique.

You will see the methods that make up an HttpServlet a little later in this hour, but first it is helpful to see how JSPs translate into Servlets and to see the lifecycle of a JSP/Servlet.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.