简体   繁体   English

将Java类显示到jsp网页中

[英]Display Java class into jsp web page

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. 您好我仍然有问题,我无法在jsp页面中显示任何数据,正如您在代码中所看到的那样,我已将java类导入到jsp中并创建了该类的对象,并调用了从java类输出的方法,但是似乎有问题..请帮助。 (please explain your advice because some hint are harder to understand - thanks) (请解释您的建议,因为一些提示更难理解-谢谢)

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>

You need to declare your output variable in class scope and add a getter method to return it. 您需要在类范围内声明输出变量,并添加一个getter方法以将其返回。
Then, after invoking the constructor of test class, use the getter method to retrieve the output string. 然后,在调用测试类的构造函数之后,使用getter方法检索输出字符串。 For example: 例如:

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

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

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对象,因为这会严重影响性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM