Android Tree Application Framework

Source: Internet
Author: User

Tomcat source Preparation before learning

Note: Because most of the online posts do not have a matching picture and error analysis, all the cost of a half-day effort to tidy up this blog, I hope everyone less detours it
Below we will start our Tomcat source learning journey. 

1. Download the Tomcat6.0 source code 

First, we have to download the source code for Tomcat6.0. The version control tool for the Tomcat source code is not CVS, but subversion, and if subversion is not installed on your machine, from http://subversion.tigris.org/servlets/ projectdocumentlist?folderid=91 Download and install this open Source version control tool. Of course, if you want to import the tomcat source directly from Eclipse, download the Subversion plugin from http://subclipse.tigris.org/update_1.0.x and import the tomcat source code. After the installation is complete, type SVN export help in the MS-DOS window and you will see: 

C:\Documents and SETTINGS\CARLWU>SVN help export 

Export: Produces a copy of the tree that has no version control. 

Usage: 1, export [-R REV] url[@PEGREV] [PATH] 

2. Export [-R REV] path1[@PEGREV] [PATH2] 

1. From the repository specified in the URL, export a clean directory tree to PATH. If there is a specified 

REV, the content is that version, otherwise it is the HEAD version. If the PATH 

If omitted, the last part of the URL will be used as the local directory name. 

2. In the working copy, export a clean directory tree to PATH2 from the specified PATH1. If 

If REV is specified, it is exported from the specified version or from the working copy. If 

PATH2 is omitted, the last part of PATH1 will be used as the local directory name. 

If REV is not specified, all local modifications are preserved, but not included in version control 

Files are not copied. 



If Pegrev is specified, it is looked up from the specified version. 

Valid options: ... 

We see that subversion has provided us with very friendly help and is Chinese, and it seems that Chinese programmers have contributed to this open source project. Next, under MS-DOS, type: 

SVN export Http://svn.apache.org/repos/asf/tomcat/tc6.0.x/tags/tomcat_6_0_0/d:\carl_wu\tomcat\src\ 

This command means to import the source code of Tomcat6.0 from the Subversion library into the D:\carl_wu\tomcat\src\ directory of this machine, and after the command runs, you wait a few minutes before you see the source code of Tomcat successfully imported into the target directory. Here is the source code of the directory organization, from this directory structure, we can see that the project developers use the IDE is eclipse, because we see the familiar. Project and. classpath files. If you are going to develop a Java application that stand alone, use the Tomcat directory structure, put the script file in the bin directory, and place the XML and properties configuration files in the Conf directory. Java source files are placed in the Java or SRC directory, resource files such as image files, INI files and other static resources files can be placed in the Res directory, test source code can be placed in the test directory. This is a typical Java application directory organization, the author has previously contacted a product from the United States, its source code directory structure and tomcat and similar. 




2. Compile and run 

After the code is downloaded, we're going to compile and run Tomcat. We can not help but think of cute ant in a compilation. Yes, Tomcat is using ant as the build tool, and if you haven't installed it, go from http://ant.apache.org/bindownload.cgi Download and install it here. Then, locate the Build.properties.default file from the Tomcat source file, copy the file to Build.properties, and then open build.properties to find the following line: 

Base.path=/usr/share/java 

Change it to: 

Base.path= D:/carl_wu/tomcat/share 

During the tomcat compilation, Ant will let us download some of the necessary dependencies, the Base.path directory is used to save these project files, we can point this property to an existing directory. After modifying Base.path, we go back to the MS-DOS window, switch to the directory where the Tomcat source code is located, and then run the ant Download command, as shown in the normal case: 


After a minute, Ant tells us a mistake and prompts us to compile the failure with the following error message: 

Downloadzip: 

[Get] Getting: Http://sunsite.informatik.rwth-aachen.de/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zip 
[Get] To:d:\carl_wu\tomcat\share\file.zip 
[Get] Error Opening Connection java.io.FileNotFoundException: Http://sunsite.informatik.rwth-aachen.de:3080/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zip 

[Get] Error Opening Connection java.io.FileNotFoundException: Http://sunsite.informatik.rwth-aachen.de:3080/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zip 

[Get] Error Opening Connection java.io.FileNotFoundException: Http://sunsite.informatik.rwth-aachen.de:3080/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zip 

[Get] Can ' t get Http://sunsite.informatik.rwth-aachen.de/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zipto D:\ Carl_wu\tomcat\share\file.zip 

BUILD FAILED 

D:\carl_wu\tomcat\src\build.xml:554:the following error occurred while executing the line: 

D:\carl_wu\tomcat\src\build.xml:514:can ' t get Http://sunsite.informatik.rwth-aachen.de/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zipto D:\ Carl_wu\tomcat\share\file.zip 

Total time:41 seconds 

This compilation error is very simple, just can't find http://sunsite.informatik.rwth-aachen.de/eclipse/downloads/drops/R-3.2-200606291905/ Eclipse-jdt-3.2.zip file. One might wonder what is the relationship between Tomcat's compilation and Eclipse's JDT? In fact, Tomcat was developed under Eclipse, so you need Eclipse's JDT (Java development Tooling) plugin to compile Tomat source code. Since can not find, we had to do their own, Google a search, immediately found that the effective of this file: http://mirror.calvin.edu/eclipse/downloads/drops/R-3.2-200606291905/ Eclipse-jdt-3.2.zip. We open the Build.properties file just now and modify its 34 lines to: 

Jdt.loc= Http://mirror.calvin.edu/eclipse/downloads/drops/R-3.2-200606291905/eclipse-JDT-3.2.zip

After modifying the Save Build.properties file, start the ant download task again. This time we wait longer, because eclipse-jdt-3.2.zip about 19M, download takes a while. We can take this opportunity to make a cup of tea to get some coffee or something, and when we come back to tea, we find that the dedicated ant Ant tells us that the build succeeds, although the compiler gives several warnings. At this point we can see that the Base.path directory (D:\carl_wu\tomcat\share) We have just created has downloaded 6 dependent projects, all of which are necessary for Tomcat compilation.


If an error occurs, check to see if the JDK version you are using is 1.5



Now that the real compilation task starts, type ant in the MS-DOS window and enter, Ant will compile more than 1000 source files in 2 minutes and deploy Tomcat to the output directory. After the compilation is complete, open the Tomcat source directory, and you will find an output directory, which is the ant's compiled export directory. Please open the Output\build\bin subdirectory of the Tomcat source code, double-click the Startup.bat file, we can successfully start the Tomcat6.0, at this time our compilation work is completed successfully.  


The directory after compilation is complete


3. Import the source code to eclipse 

3.1 Open Eclipse, create a new Java project, then click on the "Next" button, select "Create Project from existing source", And in the Directory text box, fill in the Tomcat source directory (i.e. D:\carl_wu\tomcat\src) that we just downloaded, then click "Next" until the end. 




3.2 We will see that eclipse refuses to compile because eclipse cannot find the library file specified by the project. Please right click on the project, select "Properties" à "Libraries" in the pop-up menu, then delete two two library files starting with tomcat_libs, keep only one JRE library file, then click "OK" button, At this point, Eclipse started compiling the tomcat source code, but found a bunch of errors because we didn't add the jar packages necessary for the project to compile. 

3.3 Prepare the jar files necessary for the Tomcat project, in fact, we have already downloaded these jar packages when we ran the Ant download task. (Download the Missing jar according to the DOS window prompt)

Ant.jar (please copy in the Lib subdirectory of the Ant installation directory) 

Commons-collections-3.1.jar (copied from the commons-collections-3.1 subdirectory of the ant download just now) 

Commons-dbcp-1.2.1.jar (copied from the commons-dbcp-1.2.1 subdirectory of the ant download just now) 

Commons-logging-1.1.jar (if you do not have this jar package, please download it from http://commons.apache.org/downloads/download_logging.cgi) 

Commons-pool-1.2.jar (copied from the commons-pool-1.2 subdirectory of the ant download just now) 

Org.eclipse.jdt.core_3.2.0.v_671.jar (copied from the Eclipse\plugins subdirectory of the ant download just now) 

3.4 When we have these jar files ready, copy the files to a directory (say, the D:\carl_wu\tomcat\tomcat_lib directory) and create a new user Libraries in Eclipse, we will create this new user Libraries is named Tomcat_libs and adds these files to Tomcat_libs. Then add our new tomcat_libs to the TOMCAT6 project. Also, don't forget to add the JUnit library to the TOMCAT6 project. At this point, eclipse begins to recompile, the compilation passes smoothly, and all errors disappear, and the directory structure of the TOMCAT6 project is as follows: 

Also, add the test directory to the source code by right-clicking the "test" directory in Eclipse and selecting "Build path" à "use as source Folder" in the pop-up menu, then we will see a source code symbol on the test directory. As shown in. 

3.5 Run Tomcat in Eclipse. Please find Tomcat's startup main class Org.apache.catalina.startup.Bootstrap, right click on the class, select "Run as ..." in the pop-up menu and "Open run Dialog ..." and then in the popup "Run" The window fills in the program run parameter "start" and the JVM run parameter catalina.home, as shown in the following window: 



Then click the "Run" button and we will see Tomcat start normally. Congratulations, our Tomcat source has been successfully imported into Eclipse, and visual UML analysis tools and debug tools will come in handy.    
3.5 Debugging Tomcat, open the source code for the Org.apache.jasper.compiler.Compiler class, hit a breakpoint on the first line of the Generatejava () method, Then run tomcat in the debug state of Eclipse, and after Tomcat runs, open our browser and enter http://localhost:8080/examples/jsp/jsp2/el/in the address bar Basic-comparisons.jsp and enter, then we can observe that eclipse switches to debug view at this point:  


The small experiment above shows that we can observe the internal operation mechanism of Tomcat through debugger in eclipse. In addition, the above Generatejava method is to dynamically compile the JSP into Java class, this method is only in the first request or JSP source code changes when executed, if you again in the browser to send the same request, you will not see the debug interface, Because the method is no longer executing.  
In addition, there is a bit more interesting. Tomcat6 Previous versions of the source code scattered in several sub-projects, they were called Jakarta-servletapi-5,jakarta-tomcat-5,jakarta-tomcat-catalina, Jakarta-tomcat-connectors and Jakarta-tomcat-jasper, I think the Tomcat developers might be too troublesome to do this, so the TOMCAT6 version incorporates the sub-items together. However, this approach is not conducive to our reading comprehension of source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.