Difficulty: Easy
Correct Answer: By calling response.getWriter() to obtain a PrintWriter and then writing HTML markup to it
Explanation:
Introduction / Context:
Java servlets run on the server side and generate responses that browsers display. To send HTML directly from a servlet, you must write content into the HTTP response object provided by the servlet container. New developers sometimes confuse server console output with client responses. This question checks whether you understand the correct mechanism for sending HTML to the browser.
Given Data / Assumptions:
Concept / Approach:
To send text based content like HTML, you call response.setContentType("text/html") and then get a character stream using response.getWriter(). This method returns a PrintWriter. Anything you write to this writer becomes part of the HTTP response body, which the client browser receives and renders. Writing to System.out or to a log file only produces output on the server side and does not reach the client. Similarly, creating files or comments inside code has no effect on the HTTP response unless you explicitly send their contents through the response object.
Step-by-Step Solution:
Step 1: In the servlet method, call response.setContentType("text/html"); to tell the browser that the response contains HTML.
Step 2: Obtain a PrintWriter with PrintWriter out = response.getWriter();.
Step 3: Use out.println to write standard HTML tags and content, such as <html>, <head>, <body>, and so on.
Step 4: Close the writer or allow the container to flush the output when the method returns.
Step 5: The servlet container sends the accumulated response bytes over HTTP to the client browser, which then displays the HTML.
Verification / Alternative check:
You can prove this by writing a small servlet that sets content type, gets the writer, and outputs a simple page with a heading. When you access the servlet URL in a browser, you will see that heading rendered. If you instead write only System.out.println statements, nothing appears in the browser; the text goes to the server console or log. This verifies that getWriter and response are the correct path to the client.
Why Other Options Are Wrong:
Option b incorrectly assumes that console output is sent to the browser, which is not true. Option c writes HTML to a server file but never sends it in the HTTP response, so the browser sees nothing. Option d refers to comments in Java source code, which are removed at compile time and are never transmitted to clients.
Common Pitfalls:
A frequent mistake is to mix binary output using getOutputStream with character output using getWriter in the same response, which is not allowed. Another is forgetting to set the correct content type, causing the browser to misinterpret the response. Using JSPs or template engines often simplifies HTML generation, but understanding direct output from servlets remains important.
Final Answer:
To display output directly in the browser, the servlet must use response.getWriter() to obtain a PrintWriter and write HTML markup into the HttpServletResponse.
Discussion & Comments