将 "\n" 添加到 Java String 时,换行符不会出现在 JSP 中,但会出现在标准输入中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11900314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:50:03  来源:igfitidea点击:

When adding "\n" to a Java String , the newline is not presented in a JSP but on stdin it does

javahtmljspweb-applicationsservlets

提问by Hyman cole

When I pass a java string back to my servlet , the servlet passes that string to the jsp .

当我将一个 java 字符串传递回我的 servlet 时,servlet 将该字符串传递给 jsp。

The string holds names & addresses with a new line "\n" , but when I'm trying to present that on the screen , I see a full line , however on the console the new-line is presented .

该字符串使用新行 "\n" 保存名称和地址,但是当我尝试在屏幕上显示它时,我看到了整行,但是在控制台上显示了新行。

Here's a snapshot : Firstenter image description hereSecondenter image description hereThird : from the CONSOLE

这是快照: 第一在此处输入图片说明第二在此处输入图片说明第三:来自控制台

enter image description here

在此处输入图片说明

Any idea how to solve that ?

知道如何解决这个问题吗?

Regards

问候

回答by JB Nizet

In HTML, newline characters are treated like spaces, and multiple spaces are displayed as a single space. If you want to go to the next line, you must transform newline characters into <br />, or enclose your text inside a <pre></pre>block.

在 HTML 中,换行符被视为空格,多个空格显示为一个空格。如果您想转到下一行,您必须将换行符转换为<br />,或将您的文本括在一个<pre></pre>块中。