Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


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.

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

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

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

(*) 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.

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.

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)?

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


ServletResponse#setContentType
response.setContentType("text/html;charset=UTF-8");

Any idea on the default content Type and character encoding if one is not specified explicitly?

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


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.

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

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

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


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

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


(*) 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.

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.

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)?

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


res.setContentType("image/gif");
 res.setContentType("application/pdf");
 res.setContentType("application/zip");

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

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


res.setContentType("image/gif");
 res.setContentType("application/pdf");
 res.setContentType("application/zip");

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

Note
Rectangle 27 1

java what is the use of "response.setContentType("texthtml")" in servlet?


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

Note