Wednesday, 5 March 2014

Servlet Class Hierarchy

🚀 Master Java Web Architecture & Servlets!

Want to master core Java web architecture and enterprise concepts? Subscribe to Ram N Java for more clear, beginner-friendly coding guides!

🔔 Subscribe to Ram N Java

Understanding the Servlet Class Hierarchy

In Java enterprise web development, creating custom servlets to handle web requests isn't done from scratch. Instead, Java provides a well-organized inheritance tree that separates general network communication from HTTP-specific web handling.

At the core of this structure sits an interface and two major classes forming the foundation of every servlet you write:

  • Servlet (Interface)
  • GenericServlet (Abstract Class)
  • HttpServlet (Abstract Class)

Level 1: The Servlet Interface (javax.servlet)

The Servlet interface is the top-level contract of the Servlet API. Any class that wants to be managed by a Servlet Container (like Apache Tomcat) must implement this interface directly or indirectly.

It defines the 5 primary lifecycle and utility methods:

  • init(ServletConfig config): Called once when the servlet is initialized.
  • service(ServletRequest req, ServletResponse res): Called for each client request.
  • destroy(): Called once when the servlet container shuts down or unloads the servlet.
  • getServletConfig(): Returns startup configuration information.
  • getServletInfo(): Returns information about the servlet (author, version, copyright).

Level 2: GenericServlet Class (javax.servlet)

Implementing the Servlet interface directly forces you to write boilerplate implementations for all five methods. To simplify this, Java provides GenericServlet:

  • It is an abstract class that implements Servlet, ServletConfig, and java.io.Serializable.
  • It provides default implementations for all methods except service().
  • It is protocol-independent, meaning it does not assume HTTP and can theoretically work with other communication protocols.

Level 3: HttpServlet Class (javax.servlet.http)

Since almost all modern web applications communicate via HTTP, the HttpServlet class extends GenericServlet to provide built-in HTTP request support:

  • It overrides the generic service(ServletRequest, ServletResponse) method, typecasts parameters to HttpServletRequest and HttpServletResponse, and delegates to a protected service(HttpServletRequest, HttpServletResponse) method.
  • It inspects HTTP methods (GET, POST, PUT, DELETE, etc.) and routes them to dedicated handler methods like doGet(), doPost(), doPut(), and doDelete().
  • Your custom servlets almost always extend HttpServlet and override only the specific methods needed (such as doGet or doPost).

Recommended Related Tutorials

Continue your deep dive into Java Servlet architecture with these selected tutorials from Ram N Java:



Click here to watch in Youtube : 
https://www.youtube.com/watch?v=SFgzhELIJ9Q


Click the below Image to Enlarge
Servlet Class Hierarchy

See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials