Nadjib Mami
Nadjib Mami

Reputation: 5830

Print text from a java class method within a .jsp page

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?

Upvotes: 6

Views: 22092

Answers (1)

SJuan76
SJuan76

Reputation: 24780

In a JSP, you have an implicit out object. Use out.println() to print to the web pages.

Additionaly, inside the HTML you can use <%= "Message" %> (or <% myMessage.toString() %> to the same effect

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).

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.

Upvotes: 9

Related Questions