title: How to open a file using JavaScript?
body: <p>I have a servlet that write a pdf file as a ByteArrayOutputStream to the servlet's output stream.
  If I open the servlet URL the browser opens the file.
  But if occur an error on the servlet, the browser opens an empty pdf with an error message.
  Sending an error through the ServletResponse the browser opens the default error page.</p>
  
  <p>I want to send an error message without redirecting to an error page or opening an invalid pdf file.</p>
  
  <p>I tried:</p>
  
  <pre><code>new Ajax.Request('/pdfservlet', {            
          onSuccess: function(response) {
              docWindow = window.open('','title');
              docWindow.document.open('application/pdf');
              docWindow.document.write(response);
              docWindow.document.close();
          },
          onFailure: function(response) {
              alert(response);
          }
      });
  </code></pre>
  
  <p>But, onSuccess opens a page with
  [object object]</p>
  
  <p>How can I open a PDF file using JavaScript?</p>