Java Servlets   «Prev  Next»

Lesson 10 Servlets and Web Page Generation — Conclusion
Objective Summarize the techniques and best practices for generating dynamic web pages using Jakarta Servlets.

Generating Dynamic Web Pages with Servlets

Jakarta Servlets are at the core of server-side web development in Java. They handle client requests, process data, and generate responses—often in the form of HTML or JSON. By now, you should understand not just how servlets create pages, but also how they fit within the broader Jakarta EE ecosystem that includes JSPs, filters, and template engines.

1. Key Takeaways

After completing this module, you should be able to:

  1. Generate complete dynamic web pages directly from a servlet.
  2. Combine static HTML with dynamically generated content.
  3. Maintain user state across requests using sessions or cookies.
  4. Include time-sensitive data, such as the current date and time, in servlet responses.
  5. Process form submissions and retrieve parameters from HttpServletRequest.
  6. Distinguish between request handling, redirection, and content forwarding.

2. Modern Approaches to Web Page Generation

While the classic approach involved printing raw HTML through a PrintWriter, modern Jakarta Servlet applications use a layered and modular approach to generating web pages. Here are the main strategies:

Example: Dynamic HTML from a Servlet


import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
import java.time.LocalDateTime;

@WebServlet("/greet")
public class GreetingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = resp.getWriter()) {
            out.println("<!DOCTYPE html>");
            out.println("<html><head><title>Greeting</title></head>");
            out.println("<body>");
            out.println("<h1>Hello, " + req.getParameter("name") + "!" + "</h1>");
            out.println("<p>Current time: " + LocalDateTime.now() + "</p>");
            out.println("</body></html>");
        }
    }
}

While this example demonstrates direct HTML output, best practices encourage separating logic and presentation. You might forward the data to a JSP for rendering, or use a templating framework for more complex views.

3. Core Terms and Concepts

4. Servlet Workflow Recap

The servlet lifecycle ensures that your dynamic pages are generated efficiently:

  1. The client submits a request (e.g., GET or POST).
  2. The servlet container creates HttpServletRequest and HttpServletResponse objects.
  3. Your servlet processes input, executes business logic, and generates output.
  4. The container sends the completed response back to the client.

Summary

Servlets remain foundational in Jakarta EE web development, providing direct access to HTTP mechanics and full control over the response pipeline. However, for maintainability, modern applications typically combine servlets with JSPs, template engines, or RESTful frameworks like JAX-RS to separate logic from presentation. Understanding servlet-based page generation is the first step toward mastering Jakarta EE web architecture.

The next module explores database access using servlets, demonstrating how to connect to relational databases with JDBC and manage query results safely and efficiently.

Servlets Generate Html - Quiz

Test you knowledge for generating HTML in servlets by clicking on the link below.
Servlets Generate Html - Quiz