源码调试tomcat
1.直接通过github搜索tomcat,正好找到一位有部分翻译内容的版本,直接fork并下载
git clone https://github.com/codefollower/Tomcat-Research.git
2.转换成eclipse项目工程,如果没有安装maven,可以先配置maven环境变量
sudo mvn eclipse:eclipse
3.在eclipse中导入
File->Import->General->Existing Projects into Workspace
4.导入后的效果如图所示
5.Eclipse中右击start-tomcat.launch这个文件,点Run As启动Tomcat,点Debug As可以调试Tomcat
6.效果如图所示
7.启动后通过地址http://127.0.0.1:8080/访问
8.这时候通过上文的类似Manager App打开无法访问(http://www.cnblogs.com/lixiaojiao-hit/p/5095180.html)
9.直接webapp目录下的manager host-manager docs目录放到launch/webapps目录下
10.并按照上文中的配置进行修改,在tomcat-users.xml中进行相关配置
<role rolename="tomcat" />
<role rolename="role1" />
<user username="tomcat" password="tomcat"
roles="tomcat,admin,manager,manager-gui,manager-script,manager-jax,manager-status,admin-gui" />
<user username="both" password="tomcat" roles="tomcat,role1" />
<user username="role1" password="tomcat" roles="role1" />
11.这时候访问查看manager app页面就可以访问了
12.随便点击按钮,调试一下源码,比如stop应用,审查一下源代码
13.发现访问路径为/manager/html/stop?path=/docs&org.apache.catalina.filters.CSRF_NONCE=95AC2B2A81FB328CA52AF996C7F02EB4
14.查看下后台日志,切换到apache-tomcat-8.0.30/logs目录下看下日志,tail -f 10000 localhost_access_log.2016-01-03.txt
15.那么找到web.xml中找到相关配置文件
<servlet> <servlet-name>Manager</servlet-name> <servlet-class>org.apache.catalina.manager.ManagerServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> </servlet>
16.发现相关servlet为ManagerServlet,打开ManagerServlet.java文件后发现相关代码
17.找到stop代码,直接停止到相对应的context也就是web应用
1 protected void stop(PrintWriter writer, ContextName cn, 2 StringManager smClient) { 3 4 if (debug >= 1) 5 log("stop: Stopping web application '" + cn + "'"); 6 7 if (!validateContextName(cn, writer, smClient)) { 8 return; 9 } 10 11 String displayPath = cn.getDisplayName(); 12 13 try { 14 Context context = (Context) host.findChild(cn.getName()); 15 if (context == null) { 16 writer.println(smClient.getString("managerServlet.noContext", 17 RequestUtil.filter(displayPath))); 18 return; 19 } 20 // It isn't possible for the manager to stop itself 21 if (context.getName().equals(this.context.getName())) { 22 writer.println(smClient.getString("managerServlet.noSelf")); 23 return; 24 } 25 context.stop(); 26 writer.println(smClient.getString( 27 "managerServlet.stopped", displayPath)); 28 } catch (Throwable t) { 29 ExceptionUtils.handleThrowable(t); 30 log("ManagerServlet.stop[" + displayPath + "]", t); 31 writer.println(smClient.getString("managerServlet.exception", 32 t.toString())); 33 } 34 35 }