22
public class HelloWorld extends HttpServlet{ 
      public void doGet(HttpServletRequest request, 
      HttpServletResponse response)
      throws ServletException,IOException{
      **response.setContentType("text/html");**
      PrintWriter pw = response.getWriter();
      pw.println("<html>");
      pw.println("<head><title>Hello World</title></title>");
      pw.println("<body>");
      pw.println("<h1>Hello World</h1>");
      pw.println("</body></html>");
      }
    }
Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
gowthaman
  • 983
  • 3
  • 9
  • 9
  • 6
    It basically tells the client (the webbrowser) what content type it is so that it knows what to do with it. Noted should be that the code shown so far is considered poor practice. HTML code belongs in a JSP file. See also http://stackoverflow.com/tags/servlets/info – BalusC Jan 12 '13 at 11:56

6 Answers6

22

Content types are included in HTTP responses because the same, byte for byte sequence of values in the content could be interpreted in more than one way.(*)

Remember that http can transport more than just HTML (js, css and images are obvious examples), and in some cases, the receiver will not know what type of object it's going to receive.


(*) the obvious one here is XHTML - which is XML. If it's served with a content type of application/xml, the receiver ought to just treat it as XML. If it's served up as application/xhtml+xml, then it ought to be treated as XHTML.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 2
    Where would I be able to find a listing of all content types? For instance, if I wanted to return a boolean, would I have to set it as a JSON object and then convert it accordingly (i.e. using GSON)? – tccpg288 Jan 29 '16 at 15:06
  • 1
    @tccpg288 here: https://www.iana.org/assignments/media-types/media-types.xhtml and related question: https://stackoverflow.com/questions/23714383/what-are-all-the-possible-values-for-http-content-type-header – ylka Sep 22 '17 at 08:18
14

From JavaEE docs ServletResponse#setContentType

  • 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,

response.setContentType("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.

  • This method may be called repeatedly to change content type and character encoding.

  • This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

  • Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
6

It means what type of response you want to send to client, some content types like :

 res.setContentType("image/gif");
 res.setContentType("application/pdf");
 res.setContentType("application/zip");
mtk
  • 13,221
  • 16
  • 72
  • 112
Ayaz Ali Khatri
  • 483
  • 4
  • 13
3

You have to tell the browser what you are sending back so that the browser can take appropriate action like launching a PDF viewer if its a PDF that is being received or launching a video player to play video file ,rendering the HTML if the content type is simple html response, save the bytes of the response as a downloaded file, etc.

some common MIME types are text/html,application/pdf,video/quicktime,application/java,image/jpeg,application/jar etc

In your case since you are sending HTML response to client you will have to set the content type as text/html

nagesh
  • 81
  • 5
  • In addition to telling the OP what they have to accomplish, you should actually explain how to do it (generally, this means with code.) Also, this post already has five answers, one of which is accepted, and is from four years ago. As such, I would recommend that you try to answer newer questions. – ostrichofevil Apr 02 '17 at 04:29
2
response.setContentType("text/html");

Above code would be include in "HTTP response" to inform the browser about the format of the response, so that the browser can interpret it.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
venky
  • 400
  • 1
  • 6
  • 19
1

It is one of the MIME type, in this case you are reponse header MIME type to text/html it means it displays html type. It is a information to browser. There are other types you can set to display excel, zip etc. Please see MIME Type for more information

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107