You learned about the ServletContext and the Servlet interface in the previous lesson. These two classes and their interactions are the core of understanding how Java has decided to implement request handling. However, there are a few more pieces to this puzzle. The Servlet interface has a few subclasses that handle different types of requests. This lesson will explain the most important subclasses:
- GenericServlet
- HttpServlet
What is GenericServlet?
GenericServlet is an abstract class that adds functionality to the Servlet class. It implements all the required methods except service(). This allows you to set up a custom servlet more easily since all you must do is write the service() method.
Hint: Even though it is possible to register a direct subclass of GenericServlet with the ServletContext, this is not common practice. Instead, you should extend a protocol-specific subclass of GenericServlet, such as the HttpServlet class mentioned below.
What is HttpServlet?
As you may have guessed from the name, the HttpServlet is an abstract GenericServlet subclass specifically designed to handling HTTP requests. The main difference is that this class provides these concrete methods which conform to the various HTTP methods:
doGet()doPost()doDelete()doHead()doPut()doTrace()doOptions()
Info: From now on, the above methods will be referred to as do...() methods
In essence, this structure forces you to organize your code. If you decide to allow GET requests to your servlet, you override the doGet() method, implement the required behavior, and register your new subclass with the ServletContext. The same logic follows for the rest of the do-methods listed here. That's it.
Putting it All Together
Let's wrap these two lessons together and model what happens to an incoming HTTP request.
- You instruct the application to start, and the ServletContext is created and deployed.
- The ServletContext starts all the registered servlets using
init(). - The application receives a request, and it is passed to the ServletContext.
- ServletContext creates a new instance of both ServletRequest and ServletResponse.
- The registered filters are applied to the request.
- Assuming all goes well with the filters in the previous step, the request is then dispatched to the appropriate HttpServlet using the
service(ServletRequest req, ServletResponse res)method. - The
service()method checks the HTTP method stored within ServletRequest and forwards the request to the corresponding do-method. - The do-method uses the ServletResponse instance to craft a response.
- After the HttpServlet completes execution, ServletContext can continue to process the request by applying any outgoing filters - such as compressions and encryption - and finishes by sending the request back to the outside world.
- Lastly, you instruct the application to shut down. This starts the shutdown procedure for the ServletContext which calls
destroy()for each of the initialized Servlets.
All done!
Summary: Types of Servlet
- GenericServlet is an abstract class that adds more functionality to the Servlet class.
- The HttpServlet is an abstract GenericServlet designed to handling HTTP requests.