简体   繁体   English

out.println(“ <html> 你好,世界 </html> ”)和out.println(“ H​​ello World”)

[英]difference between out.println(“<html>Hello World</html>”) and out.println(“Hello World”)

I don't have much knowledge in HTML. 我对HTML不太了解。 I am just starting to learn servlet and JSP. 我刚刚开始学习servlet和JSP。 But why are we putting HTML tags in Servlet or JSP, when it gives the same result that is put in out.println() . 但是,当HTML标记产生与out.println()相同的结果时,为什么还要在Servlet或JSP中放置HTML标记。 If you cant understand my question what is the difference between out.println("<html>Hello World</html>") and out.println("Hello World") ? 如果您无法理解我的问题, out.println("<html>Hello World</html>")out.println("Hello World")什么out.println("Hello World") Both gives the same output when the servlet is run. 运行servlet时,两者都提供相同的输出。

The difference is the type of document that you're returning. 不同之处在于您要返回的文档类型。

 out.println("Hello World")

prints plain text, and 打印纯文本,并且

out.println("<html>Hello World</html>")

returns an HTML document. 返回一个HTML文档。 It's up to the client's web browser to decide how to display it - I'd expect both to render the same. 由客户端的网络浏览器决定如何显示它-我希望两者都呈现相同的图像。

You can't see the difference until you open a source window. 打开源代码窗口之前,您看不到差异。

out.println("Hello World")

is using text/plain content-type attribute, but you should set text/html if you are using html tags. 正在使用text/plain content-type属性,但是如果使用html标签,则应设置text/html

Use the setContentType method on the ServletResponse before obtaining any object ( Stream or Writer ) used for output, eg: 在获取用于输出的任何对象( StreamWriter )之前,请在ServletResponse上使用setContentType方法,例如:

response.setContentType("text/html;charset=utf-8");

If you use a Writer , the Servlet automatically takes care of the conversion from Java Strings to the encoding selected. 如果使用Writer ,则Servlet会自动处理从Java字符串到所选编码的转换。

当您不使用html特殊命令时,没有什么区别,但是如果您喜欢使用不同字体,颜色和大小的文本,则可能还具有超链接,请使用html标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM