Initiative: To build a web component that generates dynamic HTML responses for browsers.
import java.io.*; // Import IO for writing responses
import javax.servlet.*; // Import core servlet components
import javax.servlet.annotation.WebServlet; // Import annotation for URL mapping
import javax.servlet.http.*; // Import HTTP specific servlet classes
// Use annotation to map this servlet to the /hello URL path
@WebServlet("/hello") // The server will trigger this class for requests at http://.../hello
public class HelloServlet extends HttpServlet { // Extend HttpServlet to handle web requests
@Override // Override the doGet method to handle browser GET requests
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { // Define the method signature
res.setContentType("text/html"); // Set the response type so the browser knows to render HTML
PrintWriter out = res.getWriter(); // Get the character stream writer to send text back
try { // Start a block to generate the web page content
out.println(""); // Write the HTML5 doctype declaration
out.println(""); // Start the HTML document tag
out.println("Java Web Lab"); // Set the page title in the head
out.println(""); // Start body with styles
out.println("Greetings from the Java Web Tier!"); // Print the main heading
out.println("This response was generated by a Servlet on the server."); // Print description
String time = new java.util.Date().toString(); // Get the current server-side date and time
out.println("Server Time: " + time + ""); // Print time to the user
out.println(""); // Draw a horizontal line for separation
out.println("20/20 Programs Completed. Course Compendium finished."); // Final course message
out.println(""); // End of the body tag
out.println(""); // End of the HTML document
} finally { // Ensure cleanup happens even if an error occurs
out.close(); // Close the output writer to release server resources
} // End of finally block
System.out.println("Web Log: Servlet responded successfully at " + new java.util.Date()); // Log server activity
} // End of doGet method
} // End of HelloServlet class