1. 工具
SVN、Maven、JDK1.7、MyEclipse
2. JDK1.7安装 SVN MyEclipse安装跳过
3, tomcat7.0源码下载
建目录D://tomcat7.0(其他目录都行)
目录launch 下载地址 http://apache.fayea.com/apache-mirror/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54.zip放在D://tomcat7.0的launch目录下面
目录trunk SVN地址:http://svn.apache.org/repos/asf/tomcat/tc7.0.x/trunk放在D://tomcat7.0的trunk目录下面
4 MyEclipse设置maven
Window-》Preferences-》MyEclipse-》Maven4MyEclipse
增加本地安装的maven
5 maven项目导入MyEclipse
在D://tomcat7.0添加pom.xml文件 内容如下
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.tomcat</groupId>
<artifactId>Tomcat7.0</artifactId>
<name>Tomcat7.0</name>
<version>7.0</version>
<build>
<finalName>Tomcat7.0</finalName>
<sourceDirectory>trunk/java</sourceDirectory>
<testSourceDirectory>trunk/test</testSourceDirectory>
<resources>
<resource>
<directory>trunk/java</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>trunk/test</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
Import-》Maven4MyEclipse-》Existing Maven Projects 选择D://tomcat7.0目录
6. 在Eclipse中让Tomcat跑起来
在Eclipse中打开org.apache.catalina.startup.Bootstrap类,
在编辑区右击,点”Run As->Run configurations”,然后双击”Java Aplication”就会出来一个新的”Bootstrap”,
选中它,在右边点击”Arguments”那一栏,把下面的内容copy到”VM arguments”中:
-Dcatalina.home=launch -Dcatalina.base=launch -Djava.endorsed.dirs=launch/endorsed -Djava.io.tmpdir=launch/temp -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=launch/conf/logging.properties
然后点run按钮,就可以启动tomcat了,启动成功会在Eclipse的console中显示:最后,打开你的浏览器,输入 http://127.0.0.1:8080/examples/ 看看例子吧。
7. 简单的源代码阅读指南:
- 包名 用途
- =================================================
- javax servlet/jsp/el相关的api
- org.apache.catalina tomcat自身架构
- org.apache.coyote http、ajp协议实现相关的类
- org.apache.el 实现el规范
- org.apache.jasper 实现jsp规范、编译jsp文件
- org.apache.juli tomcat的日志系统
- org.apache.naming jndi实现
- org.apache.tomcat tomcat的工具包、net、digester xml解析器
阅读顺序:
可以从org.apache.catalina.startup.Bootstrap这个类开始看起,
然后到org.apache.catalina.startup.Catalina,
在Catalina类中会触发conf/server.xml文件的解析,
这时要看org.apache.tomcat.util.digester中的类,
解析的过程中会用到org.apache.catalina.startup包中的很多RuleSet类,
server.xml文件解析完后,会生成org.apache.catalina.core包中的各种StandardXXX类的实例,
比如StandardServer、StandardService、StandardEngine等等,
这些Standard组件都是有生命周期的,接着会调用他们的init、start等方法,
会触发下面这些组件进入init、start状态
org.apache.catalina.connector.Connector
org.apache.coyote.http11.Http11Protocol
org.apache.tomcat.util.net.JIoEndpoint
在JIoEndpoint(或NioEndpoint、AprEndpoint)中会监听8080这样的端口,
有请求进来了,就进行相关的io操作,接着转到org.apache.coyote包中的相应类进行协议解析,
生成org.apache.catalina.connector.Request和org.apache.catalina.connector.Response实例,
然后转到各种Valve、应用Filter,最后到达应用的Servlet/JSP。
下图描述了Tomcat7的核心架构:
原文链接 http://developer.51cto.com/art/201207/346156.htm