Setting Response Content Type and PrintWriter in Java Servlets

Introduction

In a Java web application, a Servlet receives a request and sends a response back to the browser.
To send a proper response, two things are extremely important:

  1. Setting the response content type

  2. Writing output using PrintWriter

If these are not handled correctly:

  • The browser may not display content properly

  • Data may appear as plain text instead of HTML

  • APIs may break

This guide explains both concepts step by step, assuming no prior knowledge.

What Is Response Content Type?

Simple Definition:

Response content type tells the browser what kind of data the server is sending.

In simple words:

It helps the browser understand how to display the response.

Why Is Setting Content Type Important?

Different responses need different handling:

  • HTML pages → rendered as web pages

  • JSON → parsed by JavaScript

  • Plain text → shown as simple text

Without content type:

  • Browser guesses (often incorrectly)

  • Output may not display as expected

Common Response Content Types

Content TypeUsage
text/htmlHTML pages
text/plainSimple text
application/jsonJSON APIs
application/xmlXML data
image/pngImages

Most beginner servlets use text/html.

How to Set Response Content Type

Syntax

response.setContentType("text/html");

Always set the content type before writing output.

What Is PrintWriter?

Simple Definition:

PrintWriter is a Java class used to write text output to the browser.

In simple words:

PrintWriter sends text (HTML, JSON, etc.) from the servlet to the user.

Getting PrintWriter from Response

PrintWriter out = response.getWriter();

The HttpServletResponse object provides the writer.

Writing Output Using PrintWriter

Basic Example

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<h1>Hello from Servlet</h1>");
out.println("<p>This is servlet output.</p>");

The browser renders this as an HTML page.

Complete Example: Content Type + PrintWriter

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                     throws IOException {

    response.setContentType("text/html");

    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<body>");
    out.println("<h2>Welcome to My Servlet</h2>");
    out.println("</body>");
    out.println("</html>");
}

Explanation:

  • Content type tells browser it’s HTML

  • PrintWriter sends HTML tags

  • Browser renders the page

Sending Plain Text Response

response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("This is plain text output");

No HTML rendering—just text.

Sending JSON Response (Intro Level)

response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.println("{\"message\": \"Hello JSON\"}");

Used in APIs and AJAX calls.

Real-Life Analogy

Think of a Parcel 

  • Content Type → Label on the parcel (Fragile, Document, Food)

  • PrintWriter → The item inside the parcel

Without a label, the receiver won’t know how to handle it.

Best Practices for Beginners

  • Always set content type first
  • Use PrintWriter for text-based responses
  • Keep HTML minimal in servlets
  • Use JSP or templates for large HTML
  • Close writer if needed (out.close())

Quick Comparison Table

FeatureContent TypePrintWriter
PurposeDescribe responseSend response
ControlsHow browser reads dataWhat data is sent
MethodsetContentType()getWriter()
Used ForFormat identificationOutput writing

Read More

How to Create Database in MySQL

  • By admin
  • November 27, 2021
  • 66 views
How to Create Database in MySQL

How to create table in MySQL

  • By admin
  • November 6, 2021
  • 47 views
How to create table in MySQL

MySQL commands with examples

  • By admin
  • September 11, 2021
  • 144 views
MySQL commands with examples

MySQL use database

  • By admin
  • May 28, 2021
  • 46 views
MySQL use database

System Software

  • By admin
  • May 20, 2021
  • 71 views

Introduction to software

  • By admin
  • May 13, 2021
  • 59 views
Introduction to software