The setContentType() method must be called before using PrintWriter because it sets the content type (MIME type) of the response being sent to the client. Once the PrintWriter is obtained and used to write content, the response headers are committed and cannot be modified.