3

In servlet, normally we will specify a contentType, then we can print out the html code.

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

What IF, we don't specify a contentType, any contentType will be set default? I did a test by adding response.setContentType("text/html");, then remove after it, but my website able to render the html? Why is that?

hades
  • 4,294
  • 9
  • 46
  • 71
  • HTTP functions by before printing (for instance) HTM text sending so called header lines followed by an empty line followed by the (HTML) content. One header line can be "Content-Type: text/html". If not sent, the browser must guess itself. – Joop Eggen May 03 '16 at 08:00
  • If you are talking about browsers interpreting the response, it would sound natural that they consider it as `text/html` by default, that's what they are supposed to get most of the time . – Arnaud May 03 '16 at 08:03
  • @Berger isn't the default is text/plain? i read it somewhere – hades May 03 '16 at 08:04
  • @hades default is contentType="text/html". You can read from here https://docs.oracle.com/cd/B14099_19/web.1012/b14014/jspnls.htm – Jar Yit May 03 '16 at 08:19
  • @JarYit that's for JSP, not the Servlet API – toniedzwiedz May 03 '16 at 08:22

1 Answers1

12

There are two distinct parts to your question. Let me answer them separately

Default Content-Type header value in Java Servlet Containers

What IF, we don't specify a contentType, any contentType will be set default?

Judging by the Javadoc for ServletResponse#getContentType

Returns the content type used for the MIME body sent in this response. The content type proper must have been specified using setContentType(java.lang.String) before the response is committed. If no content type has been specified, this method returns null.

there's no default value as far as the ServletResponse is concerned. The response will simply not contain a Content-Type header.

The Java Servlet Specification (both version 2.4 and version 3) explicitly says that a Servlet container must not define a default content type.

Here's an excerpt from the Java Servlet 3.0 Specification - JSR-315, emphasis mine

Servlet programmers are responsible for ensuring that the Content-Type header is appropriately set in the response object for the content the servlet is generating. The HTTP 1.1 specification does not require that this header be set in an HTTP response. Servlet containers must not set a default content type when the servlet programmer does not set the type.

Whether you're able to observe this or not depends on external factors. There can be servlet filters in place that populate the Content-Type header, there may be a proxy between your machine and the server that does it, but all that is specific to a given application or the way it's deployed and not determined by the Servlet container.

This can be different across application servers or even depend on a piece of configuration (you can specify mappings between extensions and content types using mime-mapping in web.xml)

The bottom line is, there's a lot of ways you can set a Content-Type header in a Java Web application but as far as the Servlet API is concerned, there is no default.

Handling of responses with missing Content-Type headers by web browsers

What IF, we don't specify a contentType, any contentType will be set default? I did a test by adding response.setContentType("text/html");, then remove after it, but my website able to render the html? Why is that?

As mentioned before, something might be setting the content type along the way. Sometimes, though increasingly rarely, even if that's not the case, your web browser might still handle the response by making an educated guess as to the MIME type.

Here's what Internet Explorer/Edge does

Firefox, used to implement a mechanism called the Unknown Decoder (documentation originally placed at https://developer.mozilla.org/en-US/docs/Mozilla/How_Mozilla_determines_MIME_Types, might still be available in some web archives) back when this answer was originally posted. It no longer does that for security reasons.

Unfortunately I can't find a definitive source describing the behaviour of Chrome/Chromium. I would expect them to behave the same way Firefox does, assuming they implement the standard but I can't back that up with a citation.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131