Web Programming In Java

1. Introduction and Fundamentals: What is a Servlet?
Base definition: A Servlet is not an external framework; it is a specialized Java class that is part of the official specification (belonging to the
jakarta.servletorjavax.servletpackage depending on the version).Role in architecture and protocol: It acts as a server-side component that receives HTTP requests (Requests) from a client and generates an HTTP response (Response). The HTTP protocol regulates communication between a client computer and a server computer, specifying the content of messages (headers and body). All of this specification is translated into Java code thanks to the implementation of the Servlet interface, or more specifically, the corresponding abstract class.
Lifecycle: Managed by a web container using the
init(),service(), anddestroy()methods.
Flowchart
Sequence Diagram
2. Servlet Container
Unlike other languages like Python or Node.js/JavaScript—whose standard libraries do include classes and functions to implement a web server—Java follows a different philosophy. The Java standard offers an interface, as explained in the previous section, to which all web servers must conform if they want to be compatible with that specification. Some of the most famous ones are Tomcat and GlassFish.
To have a real servlet container (that is, one that complies with the jakarta.servlet specification and processes HttpServlet), you cannot do it solely with the base JDK classes because, as we saw, the JDK provides the interfaces but not the container engine.
3. Jetty
The easiest, most modern, and lightweight way to have a functional servlet container without manually setting up heavy servers like Tomcat or GlassFish is to use Embedded Jetty, just as we saw in the first code example.
What is Jetty?
It is a Java web server and servlet container known for being extremely lightweight, high-performing, and having a very small memory footprint compared to traditional corporate heavyweights.
Ideal Use Cases
Microservices architectures where every megabyte of RAM matters when scaling containers.
Embedded systems or devices with heavily limited hardware resources.
Why is Embedded Jetty the Easiest Way?
No XML configuration files: You don't need to deploy
web.xmlfiles or package a.warfile.Self-contained: Everything runs from a simple
mainmethod inside a.classfile or an executable.jarfile.Minimal boilerplate code: With fewer than 25 lines of code, you initialize the port, define the context, and launch the servlet container in memory.
Ultra-short Example with Maven and Embedded Jetty
If you configure a Maven project by adding only the core Jetty dependencies (jetty-server and jetty-servlet), you can instantiate the container directly like this:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import jakarta.servlet.http.*;
import java.io.IOException;
public class ContenedorMinimo {
public static void main(String[] args) throws Exception {
// 1. Instantiate the container on port 8080
Server server = new Server(8080);
// 2. Create the servlet context handler
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setContextPath("/");
server.setHandler(handler);
// 3. Add our Servlet class directly to the container
handler.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().write("Servlet Container is running!");
}
}), "/");
// 4. Start the engine
server.start();
server.join();
}
}
4. Spring, Quarkus, and Similar Frameworks
Any other alternative (such as Spring Boot) does this exact same thing under the hood, but adds automatic abstraction layers, integrating an embedded container by default (such as Tomcat or Jetty). By using annotations and controlling the object lifecycle, they hide all internal plumbing at the cost of higher startup memory consumption.
Current abstraction: Modern production tools and frameworks like Spring (and its evolution Spring Boot) build on top of the Servlet API.
Benefits of the leap: Spring Boot radically simplifies development by managing dependency injection, advanced routing, security, and configuration via annotations, shielding the developer from direct servlet complexity while maintaining the exact same underlying foundation. This is why it's so extreamily used industry.



