Java 在 JSP 页面上打印控制台输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24669786/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:58:42  来源:igfitidea点击:

Printing console outputs on a JSP Page

javajspservlets

提问by Ragesh Kr

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

例如,当我调用一个函数时connectToMyDatabase(),我有几个 syso 命令,比如

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 ??

是否可以在 jsp 页面上打印那些 system.out 输出..即显示在控制台上的输出也需要显示在 jsp 页面上。当调用该函数时,在控制台中打印的所有内容也应显示在页面上。是否可以 ??

回答by Dileep

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

将消息设置为变量消息,现在返回到jsp。而这样做,

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

回答by Santhosh

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

JSP 包含 java 代码(以 scriptlet 的形式)和 html 内容,因为两者都在浏览器中呈现。

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

所以一旦方法在java文件中执行。您可以将消息转发到 jsp 文件以检查执行情况。

By setting them in the requestor session.

通过将它们设置在requestor 中session

回答by SparkOn

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.

然后在方法执行后立即读取文件并在jsp中显示消息。

回答by iQuestProgrammer

In your Java program(servlet) set the attribute:

在您的 Java 程序(servlet)中设置属性:

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

Forward the request and response objects to the JSP page.

将请求和响应对象转发到 JSP 页面。

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

Access it in your JSP page using the getter:

使用 getter 在您的 JSP 页面中访问它:

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