You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Tomas <to...@yahoo.com> on 2008/08/16 19:05:28 UTC

Newlines in HTTP response with text/plain

Hello,

I don't know if this is the correct list for posting this question, but I have looked everywhere without finding an answer.

I have a web application that generates a String dynamically, and the user can download this as a text file.
The way to implement this is to obtain the ServletOutputStream for the response and writing the String directly, at the same time setting the "content type" for the response as "text/plain".

The problem is that the newlines are interpreted differently under Windows or Linux. If I generate the String using '\n' then the file looks wrong when opened with Notepad. And if I generate the String using '\r\n' then it's wrong when opened with vi under Linux.

I have thought about setting the correct newline using System.getProperty("line.separator"), but that uses the correct newline for the server platform, not for the client platform.

I have also thought about parsing the user agent HTTP header, but that seems to me to be too much trouble and not a robust solution.

Do you know what's the correct approach for this problem?

Thanks very much in advance.
Cheers,
Tomás



      

Re: Newlines in HTTP response with text/plain

Posted by Zigc Junk <zi...@gmail.com>.
I guess this has nothing to do with the web app. It is a problem with
the text editor you use, i.e. notepad is too windows and vi is too
unix. If you use wordpad, Textpad, JEdit, etc., you won't have any
problem.

Bill

On Mon, Aug 18, 2008 at 11:37 AM, simon <sk...@apache.org> wrote:
>
> On Sat, 2008-08-16 at 10:05 -0700, Tomas wrote:
>> Hello,
>>
>> I don't know if this is the correct list for posting this question,
>> but I have looked everywhere without finding an answer.
>>
>> I have a web application that generates a String dynamically, and the
>> user can download this as a text file.
>> The way to implement this is to obtain the ServletOutputStream for the
>> response and writing the String directly, at the same time setting the
>> "content type" for the response as "text/plain".
>>
>> The problem is that the newlines are interpreted differently under
>> Windows or Linux. If I generate the String using '\n' then the file
>> looks wrong when opened with Notepad. And if I generate the String
>> using '\r\n' then it's wrong when opened with vi under Linux.
>>
>> I have thought about setting the correct newline using
>> System.getProperty("line.separator"), but that uses the correct
>> newline for the server platform, not for the client platform.
>>
>> I have also thought about parsing the user agent HTTP header, but that
>> seems to me to be too much trouble and not a robust solution.
>>
>> Do you know what's the correct approach for this problem?
>
> I don't think there is a "correct" approach here. Webapps in general are
> not expected to have to handle differences in the receiving end. HTML is
> HTML, regardless of what OS the browser is running on.
>
> Feels a little odd to me that exactly the same url entered on two
> different operating systems could return different results for a
> downloadable file. But if you really need it, then I would think that
> parsing the user agent header would be the right way to do it.
>
> I think there are a few libraries around that parse user agent strings
> (try searching on sourceforge for example). There certainly are "browser
> sniffer" libraries for javascript. It probably isn't too hard to do it
> yourself though.
>
>
> Regards,
> Simon
>>
>
>

Re: Newlines in HTTP response with text/plain

Posted by simon <sk...@apache.org>.
On Sat, 2008-08-16 at 10:05 -0700, Tomas wrote:
> Hello,
> 
> I don't know if this is the correct list for posting this question,
> but I have looked everywhere without finding an answer.
> 
> I have a web application that generates a String dynamically, and the
> user can download this as a text file.
> The way to implement this is to obtain the ServletOutputStream for the
> response and writing the String directly, at the same time setting the
> "content type" for the response as "text/plain".
> 
> The problem is that the newlines are interpreted differently under
> Windows or Linux. If I generate the String using '\n' then the file
> looks wrong when opened with Notepad. And if I generate the String
> using '\r\n' then it's wrong when opened with vi under Linux.
> 
> I have thought about setting the correct newline using
> System.getProperty("line.separator"), but that uses the correct
> newline for the server platform, not for the client platform.
> 
> I have also thought about parsing the user agent HTTP header, but that
> seems to me to be too much trouble and not a robust solution.
> 
> Do you know what's the correct approach for this problem?

I don't think there is a "correct" approach here. Webapps in general are
not expected to have to handle differences in the receiving end. HTML is
HTML, regardless of what OS the browser is running on.

Feels a little odd to me that exactly the same url entered on two
different operating systems could return different results for a
downloadable file. But if you really need it, then I would think that
parsing the user agent header would be the right way to do it.

I think there are a few libraries around that parse user agent strings
(try searching on sourceforge for example). There certainly are "browser
sniffer" libraries for javascript. It probably isn't too hard to do it
yourself though.


Regards,
Simon
>