添加时间:[2010-3-15 8:53:44]
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
......
//取得访问路径,
String command = request.getPathInfo();
if (command == null)
command = request.getServletPath();
......
......
if (command == null) {
writer.println(sm.getString("managerServlet.noCommand"));
} else if (command.equals("/deploy")) {
if (war != null || config != null) {
deploy(writer, config, path, war, update);
} else {
deploy(writer, path, tag);
}
} else if (command.equals("/install")) {
// Deprecated
deploy(writer, config, path, war, false);
} else if (command.equals("/list")) {//找到了,就是这个路径,往下看list方法
list(writer);
} else if ......
......
} else if (command.equals("/findleaks")) {
findleaks(writer);
} else {
writer.println(sm.getString("managerServlet.unknownCommand",
command));
}
// Finish up the response
writer.flush();
writer.close();
}
//就是这个方法生成上面的那个页面
protected void list(PrintWriter writer) {
......
//host就当是当前的tomcat吧,那么contexts就此tomcat下的所有应用
Container[] contexts = host.findChildren();
for (int i = 0; i < contexts.length; i++) {//循环每个应用
Context context = (Context) contexts[i];
//应用路径
String displayPath = context.getPath();
if( displayPath.equals("") )
displayPath = "/";
if (context != null ) {
if (context.getAvailable()) {//如果应用已启动
/*打印出一行关于此应用的信息,应用的URL,当前状态,session数等,具体见上图 */
writer.println(sm.getString("managerServlet.listitem",
displayPath,
"running",
"" + context.getManager().findSessions().length,
context.getDocBase()));
} else {
writer.println(sm.getString("managerServlet.listitem",
displayPath,
"stopped",
"0",
context.getDocBase()));
}
}
}
}
public void setWrapper(Wrapper wrapper) {
this.wrapper = wrapper;
if (wrapper == null) {
context = null;
host = null;
oname = null;
} else {
//这里所有需要的对象都有了,其实下面我们需要拿到wrapper就够了
context = (Context) wrapper.getParent();
host = (Host) context.getParent();
Engine engine = (Engine) host.getParent();
try {
oname = new ObjectName(engine.getName()
+ ":type=Deployer,host=" + host.getName());
} catch (Exception e) {
// ?
}
}
// Retrieve the MBean server
mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
}
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.catalina.ContainerServlet;
import org.apache.catalina.Context;
import org.apache.catalina.Session;
import org.apache.catalina.Wrapper;
import com.esc.common.exception.BusinessException;
public class TomcatWrapperServlet
extends HttpServlet implements ContainerServlet {
private static final long serialVersionUID = 1L;
//弄个静态变量,初始化后就记下来,以备随时使用
private static Wrapper wrapper = null;
public Wrapper getWrapper() {
return wrapper;
}
public void setWrapper(Wrapper wrapper) {
TomcatWrapperServlet.wrapper = wrapper;
}
//doGet不做任何事情,只需要接收第一次请求,触发初始动作就完成它的使命了
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("Hello world!");
resp.getWriter().flush();
resp.getWriter().close();
}
//初始化后可通过此静态方法取得所有session
public static Map<String,HttpSession> fillSessions() {
if(wrapper==null){//没有初始化
throw new BusinessException("本servlet未被初始化,您必须先通过URL访问本servlet后,才可以调用这个方法");
}
Map<String,HttpSession> sessions = new LinkedHashMap<String, HttpSession>();
//取得本应用
Context context = (Context) wrapper.getParent();
//取得Session[]数组
Session[] temps = context.getManager().findSessions();
if(temps!=null && temps.length>0){
for (int j = 0; j < temps.length; j++) {
//Map<sessionId,session>
sessions.put(temps[j].getSession().getId(), temps[j].getSession());
}
}
return sessions;
}
}