Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 298
Nadjib Mami Avatar asked May 03 '12 21:05

Nadjib Mami


1 Answers

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.

like image 112
SJuan76 Avatar answered Sep 28 '22 03:09

SJuan76