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

  • Home
  • SEARCH
  • 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 618575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:33:39+00:00 2026-05-13T18:33:39+00:00

I’m developing a simple servlet that should serve documents via http. I use URLs

  • 0

I’m developing a simple servlet that should serve documents via http. I use URLs in the form of /getDocument?fileId=1234. The servlet simply 1) sets response.contentType and 2) writes to response.outputStream.

The problem is about setting the content type correctly in order to have the browsers understand the response (i.e., display the document using the correct application). Specifically:

a) For PDF files, if I set content type to “application/pdf”, Internet Explorer understands (displays the document immediately) but Firefox does not (displays blank page without attempting to open any pdf viewer plugin). If I set it to “application/x-octetstream”, Firefox understands it (displays it properly), but Internet Explorer says “unknown file type” when it asks me to save or open it.

b) Firefox understands “application/msword” and “application/vnd.ms-excel”, but Internet Explorer does, strangely, not, it simple says “unknown file type”.

Is it possible to get this to work consistently in all browsers, and if so, what is the correct way of setting content type for various document types? Is there anything else that should be set in the response in order for this do work correctly? Or, as I suspect, are the browsers getting confused when the URL does not end with the corresponding filename extension? (i.e., getFile?fileId=1234 instead of e.g. getFile/test.pdf)

  • 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-13T18:33:40+00:00Added an answer on May 13, 2026 at 6:33 pm

    Inside a servlet, the response content type ought to be set as follows:

    response.setContentType(getServletContext().getMimeType(filenameWithExtension));
    

    The ServletContext#getMimeType() lookups all <mime-mapping> entries in web.xml for the content types associated with certain file extensions. You can find all default mappings in the appserver’s own web.xml (which in case of e.g. Tomcat is located in /conf/web.xml). It might lack the “new” MSOffice OpenXML file extensions like xlsx, docx and so on. You can add them to your webapp’s web.xml like follows:

    <mime-mapping>
        <extension>xlsx</extension>
        <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
    </mime-mapping>
    

    As to how browsers handle the content type and the associated application, the major problem here is MSIE. It ignores the Content-Type header and the filename parameter of the Content-Disposition header. It instead smartass-ingly guesses the content type based on the file extension in the URL and uses the last pathinfo of the URL as filename. As you’ve used a request parameter like /getDocument?fileId=1234 instead of a fullworthy filename+extension, the filename will become getDocument and MSIE can’t reliability “guess” the mime type of it. You should really include the filename+extension in the URL like /getDocument/filename.ext. You can grab that part in the Servlet by request.getPathInfo(). For more servlet hints also see this article.

    As to the problem of your Firefox not handling PDF files correctly, this must be a misconfiguration in your Firefox. Try verifying if everything looks right in Tools > Options > Applications. It is namely supposed to respect the aforementioned headers in a correct manner. You should only ensure that any Content-Length header is correctly(!!) specified, else the file cannot be opened.

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

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer <if> is an Ant-contrib task. Have you downloaded Ant-contrib and… May 14, 2026 at 8:11 pm
  • Editorial Team
    Editorial Team added an answer std::pair uses lexicographic comparison: It will compare based on the… May 14, 2026 at 8:11 pm
  • Editorial Team
    Editorial Team added an answer Looks like you're using the wrong id to reference the… May 14, 2026 at 8:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.