java out.println() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4856976/
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
out.println() does not work
提问by palAlaa
I have homework which I have to use scriptlets in , I need to make new line in my jsp page usint out object I tried to use
我有作业,我必须在其中使用 scriptlets,我需要在我的 jsp 页面中创建新行,使用我尝试使用的对象
<%
out.println();
out.newLine();
%>
but both doesn't work !!! I treid to use
但两者都不起作用!!!我尝试使用
out.flush()
but it doesn't work!!
但它不起作用!!
回答by aioobe
Perhaps out.println("<br>");
is what you're after. (Remember that the browser in which you're viewing the jsp-page in, interprets the output of your script as HTML, which basically ignores newline characters.)
也许out.println("<br>");
这就是你所追求的。(请记住,您在其中查看 jsp 页面的浏览器将脚本的输出解释为 HTML,这基本上会忽略换行符。)
You can look at the source of the page to see what the jsp-page actually generates.
您可以查看页面的源代码,了解jsp-page 实际生成的内容。
If you reallywant to see the verbatim output of the jsp-script, you could do
如果你真的想看到 jsp-script 的逐字输出,你可以这样做
out.println("<html><body><pre>");
// ...
out.println("</pre></body></html>");
回答by Stephen C
@Alaa - out.newLine()
doeswork. It just doesn't do what you are expecting it to do ... assuming that your JSP is generating an HTML page.
@Alaa -out.newLine()
确实有效。它只是没有做您期望它做的事情……假设您的 JSP 正在生成一个 HTML 页面。
When you use out.newLine()
, it adds a newline character to the content stream that you are generating. If you use view source on the page in your web browser you can see the newline character.
当您使用 时out.newLine()
,它会向您生成的内容流添加一个换行符。如果您在 Web 浏览器的页面上使用查看源代码,您可以看到换行符。
But a newline character in an HTML document typically does notresult in a line break in the displayed page as rendered by a browser. To get the browser to render line break in the displayed page, you typically*need to output a <br />
element.
但是 HTML 文档中的换行符通常不会导致浏览器呈现的显示页面中的换行符。要让浏览器在显示的页面中呈现换行符,您通常*需要输出一个<br />
元素。
* - Actually, there are other ways to get the visual equivalent of a line break involving CSS, etcetera. And within a <pre>...</pre>
a raw newline character doesget rendered as a line break.
* - 实际上,还有其他方法可以获得与 CSS 等相关的换行符的视觉等效效果。并且在<pre>...</pre>
原始换行符中确实会呈现为换行符。
回答by Bert F
Remember the JSP code is outputting HTML. The HTML will then be rendered by the browser. A single blank line in HTML may not be shown as a blank line on the screen when the HTML is rendered.
请记住 JSP 代码正在输出 HTML。然后 HTML 将由浏览器呈现。呈现 HTML 时,HTML 中的单个空白行可能不会在屏幕上显示为空白行。
You need to either examine the HTML source in the browser and look for the blank line. Or else try output more significant HTML to verify the JSP scriptlets are working like:
您需要在浏览器中检查 HTML 源代码并查找空行。或者尝试输出更重要的 HTML 以验证 JSP 脚本的工作方式如下:
<%
out.println("<p>hello</p>");
%>