Ragesh Kr
Ragesh Kr

Reputation: 1713

Printing console outputs on a JSP Page

For example, when i call a function connectToMyDatabase(), i have few syso commands in it like

public void connectToMydatabase(){
 System.out.printl("trying to connect to db");
 /** other code goes here **/
 System.out.println("Connected successsfully"):


   }

Is it possible to print those system.out outputs on a jsp page.. ie the outputs getting displayed on the console need to be displayed on the jsp page as well. When that function is called what all gets printed in the console should get displayed on the page as well. Is it possible ??

Upvotes: 3

Views: 31510

Answers (4)

iQuestProgrammer
iQuestProgrammer

Reputation: 309

In your Java program(servlet) set the attribute:

message = "Some text here";
System.out.println(message);
request.setAttribute("message",message);

Forward the request and response objects to the JSP page.

RequestDispatcher dispatcher = request.getRequestDispatcher("file.jsp");
dispatcher.forward( request, response ); 

Access it in your JSP page using the getter:

<%= request.getAttribute("message") %>

Upvotes: 1

SparkOn
SparkOn

Reputation: 8946

One Inadvisable way is write your output using this to a file

PrintStream out = new PrintStream(new FileOutputStream("outputtest.txt"));
    System.setOut(out);

and then after the execution of the method immediately read the file and display the messages in jsp.

Upvotes: 2

Santhosh
Santhosh

Reputation: 8187

JSP contains of java-codes(in the form of scriptlets) and the html content as both are rendered in the browser.

So once the method is executed in the java file . you can forward the message to the jsp file to check for the execution.

By setting them in the request or session.

Upvotes: 0

Dileep
Dileep

Reputation: 5440

Set the message to the variable message, now return to the jsp. And do this,

<tr>"<%= request.getAttribute("message") %>"</tr>

Upvotes: 0

Related Questions