用JSP查看Tomcat运行情况

 代码如下,

 

  1. <%@page import="java.text.NumberFormat"%>
  2. <%@page import="java.util.Properties"%>
  3. <%@page import="java.math.BigDecimal"%>
  4. <%@page import="java.net.InetAddress"%>
  5. <%@page import="java.util.Enumeration;" %>
  6. <style type="text/css">
  7. image {
  8.     border: 0px;
  9.     margin: 0px;
  10.     padding: 0px;
  11.     clear: both;
  12. }
  13. .img2 {
  14.     margin-left: -3px;
  15. }
  16. </style>
  17. <table style="width: 700; text-align: left; font-size: 9pt;">
  18.     <tr>
  19.         <td>
  20.         <%
  21.         if(request.getParameter("GC") != null){
  22.           System.gc();
  23.         }
  24. Runtime lRuntime = Runtime.getRuntime();
  25. long memTotal = lRuntime.totalMemory();
  26. long memLeft = lRuntime.freeMemory();
  27. long memMax = lRuntime.maxMemory();
  28. long unasigned = memMax - memTotal;
  29. double usePercentage = (memTotal - memLeft) * 1.0/ memMax; 
  30. double usablePercentage = memLeft * 1.0 / memMax;
  31. double unasignedunasignedPercentage = unasigned * 1.0 / memMax;
  32. out.println("<div style='width: 750;'><nobr>");
  33. out.println("<image alt='Memory Used' src='images/memUsed.gif' width='" + usePercentage * 700 + "' height='10px'/>");
  34. out.println("<image alt='Memory Left' class='img2' src='images/memLeft.gif' width='" + usablePercentage * 700 + "' height='10px' />");
  35. out.println("<image alt='Memory Unsigned' class='img2' src='images/memUnsinged.gif' width='" + unasignedPercentage * 700 + "' height='10px' />");
  36. out.println("<form>");
  37. out.println("<input type='submit' value='Run GC' width='50' id='GC' name='GC' />");
  38. out.println("</form>");
  39. out.println("</nobr></div><br>");
  40. %> <image src="images/memUsed.gif"
  41.             style="width: 30px; height : 15px;" /> Memory
  42.         Used            <font
  43.             color="red"><%=round((memTotal - memLeft) * 1.0/1024/1024, 2) %></font>
  44.         MB<br>
  45.         <image src="images/memLeft.gif"
  46.             style="width: 30px; height : 15px;" /> Memory
  47.         Left              
  48.         <font color="red"><%=round(memLeft * 1.0/1024/1024, 2) %></font> MB<br>
  49.         <image src="images/memUnsinged.gif"
  50.             style="width: 30px; height : 15px;" /> Memory
  51.         Unsigned    <font color="red"><%=round(unasigned * 1.0/1024/1024, 2) %></font>
  52.         MB<br>
  53.         <br>
  54.         <br>
  55.         <br>
  56.         <br>
  57.         <%
  58. Properties pros = System.getProperties();
  59. InetAddress addr = InetAddress.getLocalHost();
  60. out.println(formatTo("Host Name:", 37) + addr.getHostName().toString() + "<br>");
  61. out.println(formatTo("Host IP:", 42) + addr.getHostAddress().toString() + "<br>");
  62. out.println(formatTo("Server OS:", 40) + pros.getProperty("os.name") + "<br>");
  63. out.println(formatTo("Server OS Version:", 33) + pros.getProperty("os.version") + "<br>");
  64. out.println(formatTo("Server Patch:", 38) + pros.getProperty("sun.os.patch.level") + "<br>");
  65. out.println(formatTo("Current User: ", 38) + pros.getProperty("user.name") + "<br>");
  66. out.println(formatTo("Java Runtim: ", 38) + pros.getProperty("java.runtime.name") + "<br>");
  67. out.println(formatTo("Java Version: ", 38) + pros.getProperty("java.runtime.version") + "<br>");
  68. out.println(formatTo("Java Vendor: ", 38) + pros.getProperty("java.vm.specification.vendor") + "<br>");
  69. out.println(formatTo("Server Encoding: ", 35) + pros.getProperty("sun.jnu.encoding") + "<br>");
  70. %>
  71. <form method="POST">
  72.     <input type="submit" id="showSessions" name="showSessions" value="Print Sessions">
  73. </form>
  74. <table>
  75. <%
  76.         if(request.getParameter("showSessions") != null){
  77.           Enumeration<String> en = session.getAttributeNames();
  78.           while(en.hasMoreElements()){
  79.             String key = en.nextElement();
  80.             out.println("<tr><td>");
  81.             out.println(key);
  82.             out.println("</td><td>");
  83.             out.println(session.getAttribute(key));
  84.             out.println("</tr>");
  85.           }
  86.         }
  87.  %>
  88. </table>
  89. <%!
  90. String formatTo(String value, int length){
  91.     String result = value;
  92.     int vL = value.length();
  93.     if(length < vL){
  94.     }else{
  95.         for(int i = 0; i < length - vL; i++){
  96.             result += " ";
  97.         }
  98.     }
  99.     return result;
  100. }
  101. double round(double v,int scale){
  102.     if(scale<0){
  103.         throw new IllegalArgumentException(
  104.             "The scale must be a positive integer or zero");
  105.     }
  106.     BigDecimal b = new BigDecimal(Double.toString(v));
  107.     BigDecimal one = new BigDecimal("1");
  108.     return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
  109. }
  110.  %></td>
  111.     </tr>
  112. </table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值