wood burning stoves 2.0*
The moose likes Servlets and the fly likes Problem with sending plain text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with sending plain text" Watch "Problem with sending plain text" New topic
Author

Problem with sending plain text

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I am trying to send a plain text document (don't ask why) to the browser from a servlet. I set the content type to text/plain but it always prompts me to save the file. Shouldn't plain text just display in the browser window?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yeah, should do. Have you tried setting the filename to so it has a .txt extension?
response.setHeader("Content-Disposition","attachment; filename=test.txt");

Sometimes it make a difference though, based on browsers trying to be too intelligent with incomming file types.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Didn't try that. I'll give it a try on Tuesday. Thanks!
Louise Haydu
Ranch Hand

Joined: Aug 17, 2000
Posts: 51
This link may shed some light...
http://ppewww.ph.gla.ac.uk/~flavell/www/content-type.html
It also mentions the *.txt extension, as recommended by David above.
[ September 01, 2002: Message edited by: Louise Haydu ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I hate IE!!!
OK, I found the problem and the cure...
It seems that if the text file is too big (not sure what "too big" is yet) then IE will not display it in the browser even if you identify it as a text file. But there is a workaround. I send a single new line character and then flush. Then I send the rest of the file. It seems that receiving the single character commits IE to displaying as text.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Heh, those wacky programmers.
Nice work-around though.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
And this works also:
response.setHeader("Content-Disposition","inline; filename=test.txt");
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
After all that, the users changed their minds and decided they would prefer to be prompted to save the file. This works to do that in IE:
response.setHeader("Content-Disposition","attachment; filename=test.txt");
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with sending plain text