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(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/plain"); response.setContentType("text/css"); // Cascading Style Sheet response.setContentType("application/html"); response.setContentType("image/gif"); response.setContentType("application/zip"); response.setContentType("application/pdf");
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.
- 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.
- 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()
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.
- Sometimes, the content type my come with character encoding specification also. Say,
- The response character encoding is set if called only before creating PrintWriter object calling getWriter() method.
- The setContentType() does not have any effect if data has been committed already or getWriter() is called.
Above "director" indicates Adobe Shockwave Player file. "x" prefix indicates the subtype is not standard (not registered with IANA, Internet Assigned Numbers Authority).
The .dxf is a file of AutoCAD. The prefix vnd indicates it is vendor specific. Another example is "application/vnd.ms-excel".
For reference Servlet architecture given.