import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Date; public class SystemMonitor extends HttpServlet { private String adjust(long val) { int mils = 0; int thou = 0; if(val > 1000000) //million { mils = (int) (val / 1000000); val = val % 1000000; } if(val > 1000) //thousands { thou = (int) (val / 1000); } if(mils > 0) { String result = "" + thou; while(result.length() < 3) { result = "0" + result; } return(mils + "." + result + "M"); } else { return(thou + "k"); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println(""); out.println("Hello World!"); out.println(""); out.println(""); Runtime runtime = Runtime.getRuntime(); int procs = runtime.availableProcessors(); String max = adjust(runtime.maxMemory()); String total = adjust(runtime.totalMemory()); String used = adjust(runtime.totalMemory() - runtime.freeMemory()); String free = adjust(runtime.freeMemory()); String hostName = request.getServerName(); String date = (new Date()).toString(); out.println("

Current Performance Statistics

"); out.println("

For " + hostName + "

"); out.println("

As of " + date + "

"); out.println("

Available processors: " + procs); out.println("
Maximum memory available (total expandable to): " + max); out.println("
Total memory allocated to JVM (used and unused): " + total); out.println("
Used memory (memory in use): " + used); out.println("
Free memory (memory unused): " + free); out.println("


Active Thread Count: " + Thread.activeCount()); out.println(""); out.println(""); } public String getRequestURL(HttpServletRequest request) { return(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI()); } }