writedby 张艳涛
有没有和我一样做到第20章的?今天基本结束了本书的阅读.最后一个案例没有demo,那我写了一回,如果有需要的可以在本地试试
路径 D:\wksp_study\designbook\server\webapps\myadmin\WEB-INF\classes\MyAdminServlet.class
D:\wksp_study\designbook\server\webapps\myadmin\WEB-INF\web.xml
以上是俩个文件,第三个文件D:\wksp_study\designbook\webapps\myadmin.xml
第一文件内容
//MyAdminServlet.java import com.zyt.tomcat.ex02.Response; import org.apache.commons.modeler.ManagedBean; import org.apache.commons.modeler.Registry; import org.apache.naming.ResourceRef; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.Set; public class MyAdminServlet extends HttpServlet { private Registry registry; private MBeanServer mBeanServer; @Override public void init() throws ServletException { registry = (Registry) getServletContext().getAttribute("org.apache.catalina.Registry"); if (registry == null) { System.out.println("Register not available"); return; } mBeanServer = ((MBeanServer) getServletContext().getAttribute("org.apache.catalina.MBeanServer")); if (mBeanServer == null) { System.out.println("MBeanServer not avaiale"); return; } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); if (registry == null || mBeanServer==null) { out.println("registry or MbeanServer not found"); return; } out.println("<html><head></head><body>"); String action = req.getParameter("action"); if ("listBeans".equals(action)) { listAllManagedBeans(out); }else if("listContexts".equals(action)){ listAllContexts(out); }else if("removeContext".equals("contextObjectName")){ String contextObjectName = req.getParameter("contextObjectName"); removeContext(contextObjectName, out); }else { out.println("Invalid command"); } out.println("</body></html>"); } private void listAllManagedBeans(PrintWriter out){ String[] managedBeans = registry.findManagedBeans(); for (int i = 0; i < managedBeans.length; i++) { out.println(managedBeans[i]+"<br/>"); } } private void listAllContexts(PrintWriter out){ try { ObjectName objectName = new ObjectName("Catalina:type=Context,*"); Set<ObjectName> set = mBeanServer.queryNames(objectName, null); Iterator<ObjectName> iterator = set.iterator(); while (iterator.hasNext()) { ObjectName obj = (ObjectName) iterator.next(); out.println(obj+"<br/>"); } } catch (Exception e) { e.printStackTrace(); } } private void removeContext(String contextObjectName,PrintWriter out){ try { ObjectName mBeanFactoryObjectName = new ObjectName("Catalina:type=MBeanFactory"); if (mBeanFactoryObjectName != null) { String operation = "removeContext"; String[] params = new String[1]; params[0] =contextObjectName; String signature[] = {"java.lang.String"}; mBeanServer.invoke(mBeanFactoryObjectName, operation, params, signature); out.println("context removed"); } } catch (Exception e) { e.printStackTrace(); } } }
第二个文件
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>myAdmin</servlet-name> <servlet-class>MyAdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myAdmin</servlet-name> <url-pattern>/myAdmin</url-pattern> </servlet-mapping> </web-app>
第三个文件
<Context path="/myadmin" docBase="../server/webapps/myadmin" debug="8" privileged="true" reloadable="true"> </Context>
访问第一个api
http://localhost:8080/myadmin/myAdmin?action=listBeans
http://localhost:8080/myadmin/myAdmin?action=listContexts