Wednesday, 5 March 2014

Servlet Packages

🚀 Master Java Web Architecture & Servlets!

Want to build a rock-solid foundation in enterprise Java web development? Subscribe to Ram N Java for more clear, beginner-friendly programming guides!

🔔 Subscribe to Ram N Java

Introduction: The Two Core Servlet Packages

When building server-side applications in Java, the Servlet API provides the standard contract between web containers (like Apache Tomcat) and your custom Java code.

The entire Java Servlet API is organized primarily into two fundamental packages:

  • javax.servlet (or jakarta.servlet in modern specifications)
  • javax.servlet.http (or jakarta.servlet.http)

Understanding how these two packages divide responsibilities helps you write clean, modular, and standard-compliant web applications.

1. The javax.servlet Package (Protocol-Independent)

The javax.servlet package provides generic, protocol-independent classes and interfaces designed to handle any type of request-response communication:

  • Servlet Interface: The root interface defining lifecycle methods (init(), service(), and destroy()).
  • GenericServlet: An abstract class implementing Servlet that provides basic setup and config handling without assuming HTTP.
  • ServletRequest & ServletResponse: Generic input/output abstractions for reading incoming data and transmitting outgoing payloads.
  • ServletConfig & ServletContext: Interfaces used to access servlet-specific initialization parameters and application-wide contextual parameters.
  • Filter & FilterChain: Components that intercept and transform requests and responses dynamically.

2. The javax.servlet.http Package (HTTP-Specific)

Because almost all web communication relies on the HTTP protocol, the javax.servlet.http package extends the generic servlet abstractions to provide rich HTTP-specific features:

  • HttpServlet: An abstract class that extends GenericServlet and automatically routes HTTP requests to dedicated methods such as doGet(), doPost(), doPut(), and doDelete().
  • HttpServletRequest & HttpServletResponse: Sub-interfaces providing HTTP methods, header parsing, cookies access, query strings, and redirect handling.
  • HttpSession: The primary interface for tracking user sessions, managing state across multiple web requests, and handling logins.
  • Cookie: A concrete class representing HTTP cookies exchanged between browser and server.

Why This Architectural Separation Matters

Java's architects deliberately decoupled protocol-independent components from HTTP-specific classes:

  1. Flexibility & Extensibility: Java Servlets were designed to theoretically support non-HTTP network protocols (such as FTP or SMTP) using the same core lifecycle.
  2. Clear Separation of Concerns: Developers building web apps can focus directly on HttpServlet while the underlying container manages generic request lifecycles cleanly.
  3. Clean Inheritance Hierarchy: HttpServlet inherits from GenericServlet, allowing HTTP-specific features to layer neatly on top of the generic servlet contract.

Recommended Related Tutorials

Expand your knowledge of Java Servlet architecture with these selected tutorials from Ram N Java:



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


Click the below Image to Enlarge
Servlet Packages

Servlet Packages


See also:

  • Servlets Tutorial
  • All Design Patterns Links
  • 1 comment:

    1. Waoo Awesome content posting,Many developers would agree that servlet is difficult and complex whereas servlet is simple and so more productive,This site is so amazing.This sites gives good knowledge of servlet.

      ReplyDelete

    Tutorials