2

I have jsp code df -h for display disk information on the website. How can I show the output to html with table? Below the code:

String[] disk;
String line;
String process;
Process p;
BufferedReader input;
    p = Runtime.getRuntime().exec("df -h");
    input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    input.readLine();
    disk = input.readLine().split("\\s+");
<%      
<tr bgcolor="#f0f0f0">
        <td colspan="2">
            <b>Disk</b>
        </td>
        <td align="center">
            <b>Size<b>
        </td>
        <td align="center">
            <b>Used<b>
        </td>
        <td align="center">
            <b>Avail<b>
        </td>
        <td align="center">
            <b>Use%<b>
        </td>
        <td align="center">
            <b>Mount<b>
        </td>
    </tr>
    while ((line = input.readLine()) != null) {
%>
        <tr>
        <td colspan="2"><% out.println(disk[0]); %></td>
        <td align="center"><% out.println(disk[1]); %></td>
        <td align="center"><% out.println(disk[2]); %></td>
        <td align="center"><% out.println(disk[3]); %></td>
        <td align="center"><% out.println(disk[4]); %></td>
        <td align="center"><% out.println(disk[5]); %></td>
        </tr>
<%
        }
    input.close(); 
%>

when on linux I execute "df -h" it showing like below:

[root@svr1 apache-tomcat-7.0.32]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/centos_svr1-root   29G  5.3G   23G  19% /
devtmpfs                      1.9G     0  1.9G   0% /dev
tmpfs                         1.9G     0  1.9G   0% /dev/shm
tmpfs                         1.9G   17M  1.9G   1% /run
tmpfs                         1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda1                     497M  163M  334M  33% /boot
tmpfs                         380M     0  380M   0% /run/user/0
[root@svr1 apache-tomcat-7.0.32]#

But at html I got like below:

Output

How I can fix it, or what code that I need to change?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
hrace009
  • 37
  • 9
  • Consider how many times the line ` disk = input.readLine().split("\\s+");` will be executed in your code. – yole Sep 22 '16 at 06:54
  • @yole it should be once, when the script execute it will show all disk information from df -h – hrace009 Sep 22 '16 at 07:02
  • @IHarrisMarfel No. Think again. – Jozef Chocholacek Sep 22 '16 at 07:14
  • `readLine().split()` reads one line of text and splits it. How many lines does `df` output? – yole Sep 22 '16 at 07:17
  • @yole at my case it show 7 lines, but it will different on every machine. – hrace009 Sep 22 '16 at 09:19
  • The important thing is that it shows more than one line. If you have code that outputs multiple lines and you only execute a statement that parses a single line once, will you get correct results? – yole Sep 22 '16 at 09:25
  • @yole yes i can get correct result, but the code will be to long, and it must static. since not all server use same amount harddrive or device, it will need to dynamic thing, so if any server execute df -h, the application can get the result – hrace009 Sep 22 '16 at 10:52
  • Sorry, I give up in trying to help you figure out the problem yourself. See my answer below. – yole Sep 22 '16 at 12:53
  • @yole thank you, it's ok, but i still trying to figure it out. – hrace009 Sep 22 '16 at 13:17
  • Try running your code in a debugger. – yole Sep 22 '16 at 13:24

1 Answers1

1

You need to move this line inside the while loop:

disk = input.readLine().split("\\s+");
yole
  • 92,896
  • 20
  • 260
  • 197