How to have line breaks in text/plain servlet response
Asked Answered
O

3

6

I have a servlet that generates some text. The purpose of this is to have a simple status page for an application that can easily be parsed, therefor I'd like to avoid html.

Everything works fine, except that linebreaks get swallowed somewhere in the process.

I tried \r \r\n \n but the result in the browser always looks the same: everything that might look like a line break just disapears and everything is in one looooong line. Which makes it next to impossible to read (both for machines and humans).

Firefox does confirm that the result is of type text/plain

So how do I get line breaks in a text/plain document, that is supposed to be displayed in a browser and generated by a servlet?

update:

Other things that do not work:

  • println

  • System.getProperty("line.separator")

Ortegal answered 15/1, 2015 at 8:6 Comment(2)
Have you tried something like PrintWriter writer = response.getWriter(); writer.println();. This will take care of line breaks of the underlying OS.Kaspar
As you say, if your HTTP response really does have a header Content-Type: text/plain then your output of carriage return and line feed should be displayed as a line break in the browser.Biauriculate
O
1

Sorry, once again it was my mistake.

We have a compressing Servlet Filter configured which removed the linebreaks.

Without that filter \n works just fine.

Ortegal answered 15/1, 2015 at 11:49 Comment(0)
S
1

in plain/text it is totally dependent on browser's word-wrap processing

for example:

in firefox

about:config

and set this

plain_text.wrap_long_lines;true

you can't handle it from server, you might want to converting it to html with plain text and line breaks or css magic

Subantarctic answered 15/1, 2015 at 8:13 Comment(3)
That would break the words at the end of the browser window, not at the places where I need the line breaks. +1 tho for the statement, that this is browser dependent ...Ortegal
try considering <wbr>Subantarctic
that would be html ... I'm actually considering using a very simple subset of html ....Ortegal
G
1

As @gnicker said, I would go for a content type of text-plain like that in your servlet:

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setContentType("text/plain");
    // rest of your code...
}

To output a new line, you just use the println function of your ServletOutputStream:

ServletOutputStream out = response.getOutputStream();
out.println("my text in the first line");
out.println("my text in the second line");
out.close

The new line character could be \n or \r\n though you could just stick with the println function.

Godthaab answered 15/1, 2015 at 8:40 Comment(0)
O
1

Sorry, once again it was my mistake.

We have a compressing Servlet Filter configured which removed the linebreaks.

Without that filter \n works just fine.

Ortegal answered 15/1, 2015 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.