Issue
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?
Solution
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
, anycontentType
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 returnsnull
.
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
, anycontentType
will be set default? I did a test by addingresponse.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.
Answered By - toniedzwiedz
Answer Checked By - Timothy Miller (JavaFixing Admin)