Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 190043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:13:22+00:00 2026-05-11T16:13:22+00:00

I have a servlet that write a pdf file as a ByteArrayOutputStream to the

  • 0

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.

I want to send an error message without redirecting to an error page or opening an invalid pdf file.

I tried:

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);
        }
    });

But, onSuccess opens a page with
[object object]

How can I open a PDF file using JavaScript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:13:22+00:00Added an answer on May 11, 2026 at 4:13 pm

    Note: I’m assuming you’re using the Prototype framework from the Ajax.Request call.

    The response object isn’t meant to be written directly, it does however, have the responseText property which should contain the returned PDF.

    Have you tried:

    new Ajax.Request('/pdfservlet', {            
            onSuccess: function(response) {
                docWindow = window.open('','title');
                docWindow.document.open('application/pdf');
                document.write(response.responseText);
                docWindow.document.close();
            },
            onFailure: function(response) {
                alert(response);
            }
        });
    

    (Notice the added .responseText)

    Edit: Okay, so that didn’t work… Try something like this:

    new Ajax.Request('/pdfservlet', {            
            onSuccess: function(response) {
                window.open('/pdfservlet');
            },
            onFailure: function(response) {
                alert(response);
            }
        });
    

    What this will do is create the ajax request, and if successful open it in a new window. Opening the new window should be fast and not actually require requesting the PDF again since the browser should have cached it during the Ajax.Request call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file upload form that is being posted back to a servlet
I have a servlet that needs to write out files that have a user-configurable
I have a servlet that construct response to a media file request by reading
I have a servlet that sends a file by setting the HTTP Content-Type to
I have a servlet that is used for many different actions, used in the
I have a servlet that I would like to run within ColdFusion MX 7.
I have many years of experience in Java including Swing, Servlet and JDBC, but
I have a servlet that acts as a proxy for fetching images by reading
I have a servlet that allows users to download (potentially large) zip files from
I'm working on an application. I have a servlet (writeDataBase.class) that writes some information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.