You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Vincent Massol <vm...@pivolis.com> on 2004/03/24 16:08:16 UTC

[Help] Servlet response's encoding of new lines, what is the correct behavior?

Hi,

I need your help. We have one test in the Cactus test suite that tests
servlet response:

    public void testGetResponseAsStringMultiLines() throws IOException
    {
        PrintWriter pw = response.getWriter();

        response.setContentType("text/html");
        pw.println("<html><head/>");
        pw.println("<body>A GET request</body>");
        pw.println("</html>");
    }

    public void endGetResponseAsStringMultiLines(WebResponse
theResponse)
        throws IOException
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        pw.println("<html><head/>");
        pw.println("<body>A GET request</body>");
        pw.println("</html>");

        String result = theResponse.getText();

        assertEquals(sw.toString(), result);

        pw.close();
    }

The problem is that this test works fine on Orion 1.x, Orion 2.x and
Resin 2.x but it fails on Tomcat 4.1.30, Tomcat 3.x and Resin 3.x.

The container where the test succeeds are those returning Windows eol
characters (\r\n) whereas those that fail return Unix eol (\n).

Is there a way (through some encoding?) to control how the response
stream should encode end of lines?

Note: I've tried using:
response.setContentType("text/html;charset=ISO-8859-1"); (this is the
default). But it doesn't help. I'm really not proficient in encoding and
I'm not sure that encodings specify how eol are built ("\n" vs "\r\n").

Any idea?

Thanks for the help
-Vincent


Re: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Vincent

In article <00...@vma>,
Fri, 26 Mar 2004 10:59:51 +0100,
"Vincent Massol" <vm...@pivolis.com> wrote: 
vmassol> > I think, the responsibility for the content body of the response
vmassol> > should be in the application, not in servlet containers.
vmassol> 
vmassol> What do you mean? Do you mean that the test is not correctly written and
vmassol> can be written so that line endings end up being the same? How would you
vmassol> do this?

If the type of end-of-line is important for the test, yes.
Of cource, I may wrong if how println() should ends line is determined
uniquely somewhere. But I don't know where it is.

I suggested "don't use println() but print()" as an idea.
I think, this is a way to control the line endigns.

Regards,
----
Kazuhito SUGURI
E-mail: suguri.kazuhito@lab.ntt.co.jp

RE: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Vincent Massol <vm...@pivolis.com>.

> -----Original Message-----
> From: Kazuhito SUGURI [mailto:suguri.kazuhito@lab.ntt.co.jp]
> Sent: 26 March 2004 05:05
> To: cactus-user@jakarta.apache.org
> Subject: Re: [Help] Servlet response's encoding of new lines, what is
the
> correct behavior?
> 
> Hi Vincent,
> 
> In article <02...@vma>,
> Thu, 25 Mar 2004 10:39:08 +0100,
> "Vincent Massol" <vm...@pivolis.com> wrote:
> vmassol> I was surprised to see that different containers yielded
> different
> vmassol> result on this. My question is: is that normal? Isn't this
> behavior
> vmassol> defined in the servlet spec? Is it possible by some setting
to
> control
> vmassol> the output of containers WRT line-endings?
> [snip]
> vmassol> But before concluding this I'd like to know if the bug is in
the
> vmassol> container's implementation or not.
> 
> As far as I know, Servlet Sepc says nothing about that.
> I think, the responsibility for the content body of the response
> should be in the application, not in servlet containers.

What do you mean? Do you mean that the test is not correctly written and
can be written so that line endings end up being the same? How would you
do this?

Thanks
-Vincent

> 
> Regards,
> ----
> Kazuhito SUGURI
> mailto:suguri.kazuhito@lab.ntt.co.jp
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Re: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Vincent,

In article <02...@vma>,
Thu, 25 Mar 2004 10:39:08 +0100,
"Vincent Massol" <vm...@pivolis.com> wrote: 
vmassol> I was surprised to see that different containers yielded different
vmassol> result on this. My question is: is that normal? Isn't this behavior
vmassol> defined in the servlet spec? Is it possible by some setting to control
vmassol> the output of containers WRT line-endings?
[snip]
vmassol> But before concluding this I'd like to know if the bug is in the
vmassol> container's implementation or not.

As far as I know, Servlet Sepc says nothing about that.
I think, the responsibility for the content body of the response
should be in the application, not in servlet containers.

Regards,
----
Kazuhito SUGURI
mailto:suguri.kazuhito@lab.ntt.co.jp

Re: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by "J. B. Rainsberger" <jb...@rogers.com>.
Vincent Massol wrote:

> Thanks to all who have sent suggestions. However, I did not phrase my
> question well enough. The goal of this test is to verify that receiving
> several lines of response content works fine. The name of the test is:
> testGetResponseAsStringMultiLines
> 
> I was surprised to see that different containers yielded different
> result on this. My question is: is that normal? Isn't this behavior
> defined in the servlet spec? Is it possible by some setting to control
> the output of containers WRT line-endings?
> 
> Of course, if the answer to this is that it's dependent on containers,
> it's not defined in the servlet spec and there's no way to control what
> the server answers, then of course, I'll modify the test so that we
> don't check for line endings.
> 
> But before concluding this I'd like to know if the bug is in the
> container's implementation or not.

Either way, it is advised to parse the response into a collection of 
lines, stripping line separators, then comparing the resulting collections.
-- 
J. B. Rainsberger,
Diaspar Software Services
http://www.diasparsoftware.com :: +1 416 791-8603
Let's write software that people understand

RE: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Vincent Massol <vm...@pivolis.com>.
Thanks to all who have sent suggestions. However, I did not phrase my
question well enough. The goal of this test is to verify that receiving
several lines of response content works fine. The name of the test is:
testGetResponseAsStringMultiLines

I was surprised to see that different containers yielded different
result on this. My question is: is that normal? Isn't this behavior
defined in the servlet spec? Is it possible by some setting to control
the output of containers WRT line-endings?

Of course, if the answer to this is that it's dependent on containers,
it's not defined in the servlet spec and there's no way to control what
the server answers, then of course, I'll modify the test so that we
don't check for line endings.

But before concluding this I'd like to know if the bug is in the
container's implementation or not.

Any idea?

Thanks
-Vincent	

> -----Original Message-----
> From: Kazuhito SUGURI [mailto:suguri.kazuhito@lab.ntt.co.jp]
> Sent: 25 March 2004 03:38
> To: cactus-dev@jakarta.apache.org
> Cc: cactus-user@jakarta.apache.org
> Subject: Re: [Help] Servlet response's encoding of new lines, what is
the
> correct behavior?
> 
> Hi Vincent,
> 
> In article <01...@vma>,
> Wed, 24 Mar 2004 16:08:16 +0100,
> "Vincent Massol" <vm...@pivolis.com> wrote:
> vmassol> The container where the test succeeds are those returning
Windows
> eol
> vmassol> characters (\r\n) whereas those that fail return Unix eol
(\n).
> vmassol>
> vmassol> Is there a way (through some encoding?) to control how the
> response
> vmassol> stream should encode end of lines?
> 
> Some ideas:
> - don't use println() but print() in both test and end methods.
> 	pw.print("<html><head/>\n");
> 
> - compare each line in end method.
> 	String result = theResponse.getText();
> 	BufferedReader reader = new BufferedReader(new
> StringReader(result));
> 	assertEquals("<html><head/>", reader.readLine());
> 
> I hope this helps,
> ----
> Kazuhito SUGURI
> mailto:suguri.kazuhito@lab.ntt.co.jp
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Re: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Vincent,

In article <01...@vma>,
Wed, 24 Mar 2004 16:08:16 +0100,
"Vincent Massol" <vm...@pivolis.com> wrote: 
vmassol> The container where the test succeeds are those returning Windows eol
vmassol> characters (\r\n) whereas those that fail return Unix eol (\n).
vmassol> 
vmassol> Is there a way (through some encoding?) to control how the response
vmassol> stream should encode end of lines?

Some ideas:
- don't use println() but print() in both test and end methods.
	pw.print("<html><head/>\n");

- compare each line in end method.
	String result = theResponse.getText();
	BufferedReader reader = new BufferedReader(new StringReader(result));
	assertEquals("<html><head/>", reader.readLine());

I hope this helps,
----
Kazuhito SUGURI
mailto:suguri.kazuhito@lab.ntt.co.jp

Re: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Kazuhito SUGURI <su...@lab.ntt.co.jp>.
Hi Vincent,

In article <01...@vma>,
Wed, 24 Mar 2004 16:08:16 +0100,
"Vincent Massol" <vm...@pivolis.com> wrote: 
vmassol> The container where the test succeeds are those returning Windows eol
vmassol> characters (\r\n) whereas those that fail return Unix eol (\n).
vmassol> 
vmassol> Is there a way (through some encoding?) to control how the response
vmassol> stream should encode end of lines?

Some ideas:
- don't use println() but print() in both test and end methods.
	pw.print("<html><head/>\n");

- compare each line in end method.
	String result = theResponse.getText();
	BufferedReader reader = new BufferedReader(new StringReader(result));
	assertEquals("<html><head/>", reader.readLine());

I hope this helps,
----
Kazuhito SUGURI
mailto:suguri.kazuhito@lab.ntt.co.jp

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org


Re: [Help] Servlet response's encoding of new lines, what is the correct behavior?

Posted by Rene Mischak <rm...@jamba.net>.
Hello Vincent,

I suggest you try System.setProperty("line.separator", "\n").

Allthough I have no perfect solution how to adopt your tests to the 
server behavior.

Just some ideas:
-set the property line.separator for the container and always force 
'\n'. Would that be possible?
-Replace '\n\r' with '\n' in the response, not very elegant.
-set the property line.separator for the test client and adjust to 
container behavior.

Hope this helps you.

Vincent Massol wrote:
> Hi,
> 
> Is there a way (through some encoding?) to control how the response
> stream should encode end of lines?
> 

IMHO, encoding does not affect the eol in any way, but setting 
'line.seperator' should do it.


-Rene