Understanding Java Servlet Architecture: UML, Execution Process, Roles, and Lifecycle
This article explains the core concepts of Java Servlets, including their UML relationships, execution flow within a container, dual role as page and controller, and the complete lifecycle from loading to destruction, providing essential knowledge for backend development.
In Java Web development, Servlets serve as a fundamental component; mastering them provides a solid foundation for advanced frameworks such as SSH, SSM, and micro‑service architectures.
UML Overview
The Servlet class hierarchy consists of the abstract class HttpServlet extending GenericServlet, which implements the Servlet, ServletConfig, and Serializable interfaces. User‑defined servlets (e.g., MyServlet) extend HttpServlet and override doGet() and doPost().
Execution Process in a Servlet Container
A request from a browser (e.g., a GET request) is received by the container, which creates HttpServletRequest and HttpServletResponse objects, locates the target servlet, spawns a thread, and invokes the servlet’s service() method. Depending on the request type, service() delegates to doGet() or doPost(). After processing, the thread is returned to the pool or destroyed.
Key notes: each servlet typically has a single instance; each request is handled by a separate thread; multiple threads may operate on the same servlet, which can lead to thread‑safety issues.
Servlet’s Role in JavaWeb
Servlets act both as pages and as controllers. With the advent of JSP and other view technologies, the controller role becomes dominant, forming the classic MVC three‑layer architecture (JSP + Servlet + Model).
Example of a doGet() implementation:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("Hello!Servlet.");
}Servlet Lifecycle
The lifecycle consists of loading the servlet class, instantiating the servlet (executing the no‑arg constructor), invoking init() once, handling requests via service() (which calls doGet() or doPost()), and finally calling destroy() before the servlet is removed.
Understanding these concepts equips developers with the knowledge needed to build robust, scalable backend services using Java Servlets.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
