将Java类显示到jsp网页中

时间:2021-07-18 22:15:13

Hello I still have a problem, I can't display any data in jsp page, as you can see in the code i have imported the java class into jsp and created an object of the class and called the method for output from java class but it seems something wrong ..help please. (please explain your advice because some hint are harder to understand - thanks)

你好我还有问题,我无法在jsp页面中显示任何数据,你可以在代码中看到我已经将java类导入jsp并创建了该类的对象并调用了java类输出的方法但是好像有点不对..请帮忙。 (请解释一下你的建议,因为有些提示更难理解 - 谢谢)

package mydata;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class test {
    public test() {
        Sigar sigar = new Sigar();
        String output = " ";
        CpuInfo[] cpuInfoList = null;
        try {
            cpuInfoList = sigar.getCpuInfoList();
        } catch (SigarException e) {
            e.printStackTrace();
            return;
        }
        for (CpuInfo info : cpuInfoList) {
            output += "\nCPU\n";
            output += "Vendor: " + info.getVendor() + "\n";
            output += "Clock: " + info.getMhz() + "Mhz\n";
            output += "Core: " + info.getCoresPerSocket();
        }
        System.out.println(output);
    }
    public static void main(String[] args) {
        test main = new test();
    }
}

//JSP Code
<%@page import="mydata.test"%>
<%@page import="org.hyperic.sigar.Sigar"%>
<%@page import="org.hyperic.sigar.CpuInfo"%>
<%@page import="org.hyperic.sigar.SigarException"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <h1>Welcome to data page</h1>
        <%@ page import="mydata.test.*"%>
        <%

        String output="";
        CpuInfo[] cpuInfoList = null;
        test ts = new test();
        Sigar sigar = new Sigar();
        out.println(output);
        out.println(sigar.getCpuInfoList());
%>
    </body>
</html>

2 个解决方案

#1


You need to declare your output variable in class scope and add a getter method to return it.
Then, after invoking the constructor of test class, use the getter method to retrieve the output string. For example:

您需要在类范围中声明输出变量并添加getter方法以返回它。然后,在调用测试类的构造函数之后,使用getter方法检索输出字符串。例如:

// Java
public class test {
    private String output;
    public String getOutput() {
        return this.output;
    }

// JSP
<%
    test ts = new test();
    out.println(ts.getOutput());
%>

#2


The output stream used in a servlet, to output the content is different than standard output stream (out != System.out).

servlet中用于输出内容的输出流与标准输出流(out!= System.out)不同。

There are two ways to solve your problem:

有两种方法可以解决您的问题:

  1. Pass the output stream object to your test method in your class:

    将输出流对象传递给类中的测试方法:

    test(OutputStream out) {
        ...
        out.println(output);
    }
    
  2. Return your output from test method:

    从测试方法返回输出:

    StringBuilder buffer = new StringBuilder();
    for (...) {
        buffer.append(...);
    }
    return buffer.toString();
    

I suggest using StringBuilder instead of concatenating the String objects since this can significantly affect performance.

我建议使用StringBuilder而不是连接String对象,因为这会显着影响性能。

#1


You need to declare your output variable in class scope and add a getter method to return it.
Then, after invoking the constructor of test class, use the getter method to retrieve the output string. For example:

您需要在类范围中声明输出变量并添加getter方法以返回它。然后,在调用测试类的构造函数之后,使用getter方法检索输出字符串。例如:

// Java
public class test {
    private String output;
    public String getOutput() {
        return this.output;
    }

// JSP
<%
    test ts = new test();
    out.println(ts.getOutput());
%>

#2


The output stream used in a servlet, to output the content is different than standard output stream (out != System.out).

servlet中用于输出内容的输出流与标准输出流(out!= System.out)不同。

There are two ways to solve your problem:

有两种方法可以解决您的问题:

  1. Pass the output stream object to your test method in your class:

    将输出流对象传递给类中的测试方法:

    test(OutputStream out) {
        ...
        out.println(output);
    }
    
  2. Return your output from test method:

    从测试方法返回输出:

    StringBuilder buffer = new StringBuilder();
    for (...) {
        buffer.append(...);
    }
    return buffer.toString();
    

I suggest using StringBuilder instead of concatenating the String objects since this can significantly affect performance.

我建议使用StringBuilder而不是连接String对象,因为这会显着影响性能。