从 .jsp 页面中的 java 类方法打印文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10439723/
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
Print text from a java class method within a .jsp page
提问by Nadjib Mami
I'm just experiencing JSP form Java, I'm using a Java *.java class, within this class there's a method that print a String System.out.println("Message");
, I call this method from index.jsp, the message "Message" appears on the console of the server but not in the index.jsp because System.out.println();
won't work on a jsp file.
Edit: The question is obvious how to send and show this message in my index.jsp?
我只是在体验 JSP 形式的 Java,我使用的是 Java *.java 类,在这个类中有一个打印 String 的方法System.out.println("Message");
,我从 index.jsp 调用这个方法,消息“Message”出现在控制台上服务器但不在 index.jsp 中,因为System.out.println();
不适用于 jsp 文件。
编辑:问题很明显,如何在我的 index.jsp 中发送和显示此消息?
回答by SJuan76
In a JSP, you have an implicit out
object. Use out.println()
to print to the web pages.
在 JSP 中,您有一个隐式out
对象。使用out.println()
打印到网页。
Additionaly, inside the HTML you can use <%= "Message" %> (or <% myMessage.toString() %> to the same effect
此外,在 HTML 中,您可以使用 <%= "Message" %> (或 <% myMessage.toString() %> 达到相同的效果
UPDATE:
更新:
Either you are in the JSP (or servlet) or you are not. The object that receives the stream to write the HTML is a servlet* (explicit or being compiled from JSP). If you can write from some other class, you need to pass the out
to that class and use it (you cannot write to the web page with System.out).
您要么在 JSP(或 servlet)中,要么不在。接收流以编写 HTML 的对象是一个 servlet*(显式或从 JSP 编译)。如果您可以从其他某个类写入,则需要将 传递out
给该类并使用它(您不能使用 System.out 写入网页)。
Be careful not to pass it to your bussiness logic class, these should be UI agnostic (i.e. they do not have to know that the UI is HTML); it would be bad practice as it would mix internal classes with external output.
注意不要将它传递给你的业务逻辑类,这些应该是 UI 不可知的(即他们不必知道 UI 是 HTML);这将是不好的做法,因为它会将内部类与外部输出混合在一起。