首页 文章

在jsp中显示进程

提问于
浏览
-1

当我在jsp中显示进程时遇到问题我使用以下代码:

public ArrayList<String> getProcessusList(){

     ArrayList<String> ss=new ArrayList<String>();
     String st="";
 try {
        String line;
        Process p = Runtime.getRuntime().exec
                (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
        BufferedReader input =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            String[] se=line.split(" =\n");
            for(String sd:se){ss.add(sd);}
        }


        input.close();
    } catch (Exception err) {
        err.printStackTrace();
    }
 return ss;
}

在jsp文件中,代码是:

<body>
  ArrayList <String>processArray=p.getProcessusList();
  <%for(String se:processArray){
  String []s=se.split(" ");
  for(String sd:s){%><%=sd %>&nbsp;<% }%> <br><% } %>


  </body>

输出:

enter image description here

但是我想要一种更加用户友好的格式,你能帮助我吗?

4 回答

  • 0

    我的解决方案,一些硬代码:

    public ArrayList<String> getProcessusList(){
    
            ArrayList<String> ss=new ArrayList<String>();
            try {
                String line;
                Process p = Runtime.getRuntime().exec
                        (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
                BufferedReader input =
                        new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    String[] se=line.split(" =\n");
                    for(String sd:se){
                        String[] sa = sd.split("\\s\\s+");
                        if (sa.length>1)
                            ss.add(sd);
                    }
                }
                input.close();
            } catch (Exception err) {
                err.printStackTrace();
            }
            return ss;
        }
    

    JSP文件:

    <%
    ArrayList<String> processArray=p.getProcessusList();
    %>
    <table border="1" cellspacing="0">
        <thead>
            <tr>
                <%
                String se = processArray.get(0);
                String[] sa = se.split("\\s\\s+");%>
                <%for (int j=0;j<sa.length;j++){ 
                if(j==1){%>
                <th>PID</th>
                <th>Session Name</th>
                <%} else {%>
                <th><%=sa[j] %></th>
                <%}} %>
            </tr>
        </thead>
        <tbody>
            <%for (int i=1;i<processArray.size();i++){ 
            se = processArray.get(i);
            sa = se.split("\\s\\s+");%>
            <tr>
                <%for (int j=0;j<sa.length;j++){
                if(j==1){
                    String ssa[] = sa[j].split(" ");%>
                <td><%=ssa[0] %></td>
                <td><%=ssa[1] %></td>
                <%}else{ %>
                <td><%=sa[j] %></td>
                <%}} %>
            </tr>
            <%} %>
        </tbody>
    </table>
    
  • 0

    获取 HTML table 并添加行( tr )和 td's 以获得正确的结构 .

    此外,你可以使用CSS做一些 styling .

    在jsp中使用scriplets已经过时且非常不鼓励 . 使用JSTL而不是Scriplets

    模拟代码

    <c:forEach items="${element}" var="myCollection">
    
        <tr>
            <td><c:out value="${element.field}"/></td>
    
        </tr>
    </c:forEach>
    

    更喜欢阅读:How to avoid using scriptlets in my JSP page?

  • 0

    添加html表(或div)并使用JSTL生成它 .

  • 1

    试试StringUtils

    StringUtils.rightPad(sd,30);
    

相关问题