0

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)

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>
3
  • And where is the Sigar class with the sigar.getCpuInfoList() method? You are printing: out.println(output); which is "" and then out.println(sigar.getCpuInfoList()); Commented May 4, 2015 at 6:53
  • 1
    Don't use Java code in JSP page. It's bad habit instead use JSTL. Commented May 4, 2015 at 6:55
  • what's wrong? Any exception/error? Commented May 4, 2015 at 6:57

2 Answers 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:

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

// JSP
<%
    test ts = new test();
    out.println(ts.getOutput());
%>
Sign up to request clarification or add additional context in comments.

Comments

0

The output stream used in a servlet, to output the content is different than standard output stream (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.

1 Comment

Thanks Regulus for your help I have tired your notice but also seems nothing come out (data out put) ? I'm trying all the notice and help for 2 weeks with no result ? if I run the code as java application is work fine BUT I can't pull any data into any web page and this what i'm looking for but unfortunately so far no way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.