setContentType() Method Example

We know when a Servlet is loaded, the Servlet container creates an object of Servlet, calls service() method, creates an object of HttpServletRequest and also an object of HttpServletResponse and passes these objects to service() method.

The job of HttpServletResponse (referred as response object in this discussion) is to pass data required by the client from the Web server. To do the job, the response object comes with many methods of which one is setContentType() method.

setContentType() Introduction

setContentType(String) is defined in ServletResponse interface and inherited by HttpServletResponse interface.

Let us see first what Servlet API says about this method

    void setContentType(java.lang.String type): Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response’s character encoding is only set from the given content type if this method is called before getWriter is called.

Before sending data to client (displayed by Browser on client machine), the Servlet container informs the client browser of what type of data is being sent now. The data that can be sent may be simple plain text, html form, xml form, image form of type gif or jpg, excel sheet etc. To send this information, the Servlet container uses response object with the method setContentType().

Note: The setContentType(String) method takes a string parameter and does not return anything (returns void).

Some examples on setContentType()
response.setContentType("text/html");           // the most popular one
response.setContentType("text/css");            // Cascading Style Sheet

In "text/html", "text" is known as type and "html" known as subtype. A type contains many subtypes. It is how the data is grouped. Who has set it? Is’it Servlets or ASP? No, not anyone of them. It is set by Internet Authority to set data for Internet purpose in the name of MIME (Multipurpose Internet Mail Extension) type. All these formats of types and subtypes are set originally for Internet usage. Servlets use this type of nomenclature to inform the client what data is being set. This, in Servlets, is known as setting the MIME type.

As per this MIME type information, the client’s browser formats the data being sent by Server.

After setting the content type only, the data sent by the server should be commited.

More Definitions

  1. HTTP response sends data to client in byte stream format. Content type tells the client how this byte stream is to be interpreted and displayed to client.
  2. HTTP protocol can transport data other than HTML format also. Content type tells client what to do with the data.

setContentType() sets the content type of response being delivered, provided the response is not yet committed (not sent).

Special Notes on setContentType()
  1. application/xml
    Sets the content type to XML. If the server would like to send XHTML, then modify as "application/xhtml+xml" and now browser treats as XHTML.
  2. Sometimes, the content type my come with character encoding specification also. Say,
    response.setContentType("text/html; charset=UTF-8");
  3. The response character encoding is set if called only before creating PrintWriter object calling getWriter() method.
  4. The setContentType() does not have any effect if data has been committed already or getWriter() is called.
  5. application/x-director
    Above "director" indicates Adobe Shockwave Player file. "x" prefix indicates the subtype is not standard (not registered with IANA, Internet Assigned Numbers Authority).
  6. image/vnd.dxf

    .dxf is a file of AutoCAD. The prefix vnd indicates it is vendor specific. Another example is "application/".

For reference Servlet architecture given.


2 thoughts on “setContentType() Method Example”

Leave a Comment

Your email address will not be published.