• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incorrect file type when creating file from servlet

 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm automatically generating an xml file using a servlet. However, I need to keep the file extension, so I can't use html/jsp/etc. So in my web.xml I mapped a servlet to file.xml

This works except for one thing. The formatting is off. IE seems to think it's an html file and only shows the text within the brackets, not the actual xml elements. When I view source, my entire text is there. And when I try and save it, it wants to save as an html file with a name of file_xml

If I just drop file.xml into my WebContent folder, everything works as it should. So I'm assuming that the servlet is outputting the incorrect content type(namely none). But I've tried both text/plain and text/xml to no avail. Is there someway I can tell how tomcat hosts the file regularly so I can modify my servlet to output the correct options?

Or barring that, does anyone know what I need to change in order to have the output render correctly?

TIA.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34974
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bai Shen wrote:I'm automatically generating an xml file using a servlet. However, I need to keep the file extension, so I can't use html/jsp/etc. So in my web.xml I mapped a servlet to file.xml

Why not? You can use a JSP as the view for any text base file type. I've done .txt, .csv, etc. You do have to set the response header, but that can be done in a JSP.
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, ordinarily if you had a servlet generate XML (or what ever) contents, you would be able to set the Content-Type header into the response, inside the doGet() or doPost() methods before you started writing out your file contents.

I'm not sure how to configure tomcat to have a file mapped as a servlet be of a special content type. By default if the file content type would be the text/html

I guess you could make a content serving servlet, that just does the reading of the file and streaming it back. Thats kind of heavyweight for if you already have an xml file thought.

Unless, you are dynamically generating the xml output from a servlet already, then just doing a response.setContentType() before starting to write back any output would likely add the content type.

The other idea I can think of now is to create a Filter that just staples a content type header onto the response. This would work on top of the servlet, or even a plain file, what ever URL the filter is mapped to.



oh the other thing, when you are generating the xml content, are you doing that XML preamble thing on its first line ?


I.E. (because it is a horrible browser), might just be confused if it doesn't see that preamble thing, even though the Content-Type HTTP header is set on the response.

Another idea, before using this filter thing, use Firefox browser, with the Live HTTP headers plugin,(and/or firebug) to sniff the response, to see what exactly is the Content-Type header being sent.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Bai Shen wrote:I'm automatically generating an xml file using a servlet. However, I need to keep the file extension, so I can't use html/jsp/etc. So in my web.xml I mapped a servlet to file.xml

Why not? You can use a JSP as the view for any text base file type. I've done .txt, .csv, etc. You do have to set the response header, but that can be done in a JSP.


I'm not sure what you mean here. I need the background stuff to be transparent to my users. I want this to work exactly as if they were connecting to a regular file and saving it. ie. http://server/app/file.xml and then they end up with file.xml on their desktop.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Hein wrote:Right, ordinarily if you had a servlet generate XML (or what ever) contents, you would be able to set the Content-Type header into the response, inside the doGet() or doPost() methods before you started writing out your file contents.


I've tried that, but it doesn't seem to do anything.

I guess you could make a content serving servlet, that just does the reading of the file and streaming it back. Thats kind of heavyweight for if you already have an xml file thought.


I don't already have an xml file. I'm dynamically generating the xml. But that causes the browser to treat it as html. So I made a copy of one of the dynamically generated xml and saved it to a file on the server. That works, but it's not what I need.


oh the other thing, when you are generating the xml content, are you doing that XML preamble thing on its first line ?


I.E. (because it is a horrible browser), might just be confused if it doesn't see that preamble thing, even though the Content-Type HTTP header is set on the response.


Yup. It's fully formed, valid xml. It just shows up as html when served from a servlet.

Another idea, before using this filter thing, use Firefox browser, with the Live HTTP headers plugin,(and/or firebug) to sniff the response, to see what exactly is the Content-Type header being sent.


Hadn't thought of that. I'll give it a try.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it suddenly started working. I have no idea why. It's the same setup as before, but now it's working. Thanks anyways.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now it's broke again. I'm not sure what the issue is. It seems to switch between the two behaviors with no rhyme or reason.
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stumped.

would setting a content-disposition http header help ?

http://support.microsoft.com/kb/260519
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic