Monday, June 30, 2008

Change font in PDT

The way to change font in PDT is not intuitive.  PDT does not support configuration of font itself. Instead, it inherits global configuration. By click "windows->preference->Apperance->Colors and Fonts", you will get:
image 

Although there are two entries related to PHP, they are useless in fact. You can not configure them. Instead, you may think configuration of entry "Basic" should work. But, you are wrong. You should configure entry "Structured Text Editors" to configure fonts which will be used by PDT PHP editor. I don't why PDT chooses to inherit font setting from "Structured Text Editors".

PHP development environment

[PHP IDE]
PHPEclipse: A plug-in of Eclipse.
PDT: a new PHP IDE which is also a plug-in of Eclipse. It is supported by Zend.
Aptana with PHP plug-in: Aptana is excellent tool to develop web2.0 applications. It excels at CSS + JS... I don't know whether its support for PHP is good as well.

[PHP Debug]
XDebug: This is a PHP extension which provides a lot of valuable debug information. Its installation is fairly simple. Users just need to download the extension, put it into PHP ext directory and modify php.ini file.
DBG PHP Debugger:
Zend Debugger: I googled and found this debugger. But I could not find related documents. So actually I don't know how to use it.

[PHP Test]
SimpleTest: A unit testing tool for PHP.

Finally, I selected PDT+Eclipse as my IDE. I found this article which describes how to debug in PDT. PHP programs can be debugged locally or on a remote server. PDT supports Zend Debugger and XDebug. PDT all-in-one package is suitable for those who want a new Eclipse+PDT+Prerequisites. If you have already installed Eclipse, you can just install PDT as plug-in. The latter is what I chose. After installing PDT, you need some configurations.
Instructions about how to configure and use all-in-one PDT package are listed here.

[Configuration of PDT]
Procedure to build PHP PDT environment not using all-in-one package:
(1) Install PHP and Debugger
To use PDT, first install PHP(I am using PHP 5.2.6). Then install corresponding PHP debugger module you choose(xdebug or zend debugger). My PHP version is 5.2.6 and I downloaded XDebug module from here. Then you need to configure PHP (modify php.ini) to use the debugger. Installation instructions about XDebug is here. After configuration, restart your web server to see whether XDebug is configured correctly. Function phpinfo() can be used. Or you can use command "php.exe -m" to list the loaded modules and XDebug should be in the Zend Module list.
(2) Configure PDT in Eclipse
If you don't use PDT all-in-one package, you need lots of configuration work.
There are two modes in which PHP can be run: script mode and web mode.
In web mode, you need a web server(e.g. Apache httpd) to host PHP applications and you can test your apps by accessing those PHP pages through browser. Generally, this is the way we publish our PHP programs when we finish.
In script mode, your PHP programs can be run by an independent executable file. PHP package provides an executable file called php.exe which can be used to execute your PHP program.
image
The most important configuration options are "Debug", "PHP Executables", "PHP Servers". 

[Run/Debug]
You can debug and run your PHP program in both modes in Eclipse. When you open Run/Debug dialog in Eclipse, following dialog should be displayed:
image
Note the two entries within red box. If you want to debug/run program as script, you should configure "PHP Executable" correctly. If you want to debug/run program on a web server, you should configure "PHP Server" correctly. Meanwhile, you should select the debugger you would like to use. This must match the debugger module you actually installed. If you install a Zend debugger for PHP while you set XDebug as your debugger in Eclipse, something unpredictable may occur.
You also can set the debug scope by clicking tab "Advanced":
image
Usage of these options is(The explanation is from help document of PDT):

  • Debug All Pages - The specified page and all the pages linked to it are debugged. The browser waits for the debug of each page before displaying it.
  • Debug First Page Only - Only the first page is debugged.
  • Start Debug from - Select the URL from which you would like the Debugging process to start.
  • Continue Debug from this Page - Selecting this option will result in all the pages linked to the URL being debugged.

When you debug PHP program in web mode(In script mode, you don't have this concern.), you must explicitly terminate the debugging process. When you click button "resume" to make the program execute until completion, the program actually does not terminate. One bug? or annoying thing is that the "Terminate" button in debug toolbar is disabled which gives users illusion that the debugged program is terminated. This illusion is natural because users don't have way to terminate the debugged program. However, the debugged app is still alive and waiting for new incoming requests. One way to terminate the debugged app is that: once more you access the debugged web page, and when the debugger stops at a breakpoint, click button "Terminate"(not button "Resume") to truly kill the debug process. Another way is that when you click an arbitrary statement in window "Debug", button "Terminate" is activated and enabled.

[Deploy]
To test programs in web mode, a web server is necessary. In my case, web server is located in my local machine as well. One way to deploy the program during development is configure Apache httpd to change its document root to point to your development root directory. However, this is not good because other applications which don't reside in your current development root will not work. My solution is to use HTTP Server configuration in Eclipse. First, I add a new HTTP server to Eclipse.
image 
Then I add the PHP application to that HTTP server:
image
When I change a source file, Eclipse will automatically publish newest version of the file to the directory specified in HTTP Server configuration. I let it point to my original Apache Httpd document root.

[Issues]
One issue I have not figured out is how to use the "Path Mapping" when PHP Server is set:
image
I don't know what that means. Hopefully, I will figure it out tomorrow.

Now, I know how to use Path Mapping.
When you run/debug your PHP page on web server, PDT needs a mapping relationship between remote document directory and current working directory. As you all know, client can not retrieve PHP source file by accessing it in browser. What is returned is output produced by PHP interpreter and web server. In other words, PDT has no way to get the PHP file you want to run/debug from remote web server. Instead, you must maintain a copy in local directory.
In my case, remote PHP URL is at http://localhost/php (I run web server on my local machine). And my Eclipse PHP working directory is different. Then I create a path mapping: "E:\Program Files\Apache Software Foundation\Apache2.2\htdocs\php\TestPHP" => "/TestPHP"(Path in Eclipse workspace, you also can use absolute path in file system). When I access http://localhost/php/TestPHP/test.php, the file /TestPHP/test.php is displayed and traced in Eclipse debug window. The php file on remote server is debugged and local PDT communicates with remote server/debugger by specific debugging protocol. Apache http server will check the existence of requested php file before it transfers processing control to PHP interpreter. If no corresponding php file is found by web server, error message is returned. If content of file on remote server and content of local file is different, weird behaviors may occur. Generally, they should be identical.  To sum up, debugging occurs on remote server and PDT uses local php source file to display debug information and let user control (step in, step over...) the progress of debugging.

Some Useful Open Source Screen Capture Apps

When I write an article on my blog using WLW, generally I need to insert some links, pictures... WLW provides good support for insertion of various resources(link, picture, map, tag, video, code snippet...). However, I still need some standalone software which can make my work more convenient. Sometimes, I need to take a screen shot. Using PrtSc key + MS Paint is a solution which I have been using. However, more powerful tool is preferred. During I surfed on the web, I found some valuable software. And something important is that they are free!!!

Wink: A excellent screen capture software which eases creation of tutorial and presentation. Moreover, users can add explanations, buttons etc to the tutorial/presentation. Supported output formats are .swf, .exe, .pdf, .html and postscript. Users can customize the region to capture. One nice feature is input-driven capture. It means tha Wink only captures the specific region when a user clicks/moves mouse or strokes a key. This feature enhances performance obviously and makes output file size smaller because it removes some possible duplicate frames.

CamStudio: Another free screen capture software. I haven't used it so I will not say more about it.

XVidcap: Another good one.

XnView: A good and simple app which can manage your pictures. Besides, it provides some extra functionalities like simple picture processing(crop, resize, slideshow...)... One interesting feature is that it supports screen capture. At first glance, I think it is just a simple add-on feature which does not make much sense compared to other excellent features. However, when I used it, It provides a good feature which greatly solves my problem. Sometimes, I want to capture a specific window(e.g. Explorer, Browser...). Usual way is to capture whole desktop and then crop it in MS Paint. This procedure is tedious. XnView's screen capture provides a way to capture specific window. Moreover, you can exclude the title bar of the window... See the screen shot
image 

There are many more screen capture software out there on the web, e.g. ZScreen, Greenshot, capturedit, Sigis, AutoScreen, Taksi, Jacareto...
The basic functionality of these software is capture of entire window, customized region and a specific window. Some apps provide simple image edit features. I found Greenshot is what I want.

Finally, I found something ironical. Windows built-in screen capture support capture of active window.
(1) When you press PrtSc key, entire desktop is captured.
(2) When you press Alt + PrtSc key, only active window is captured.
There some useful tips about screen capture here.

 

Sunday, June 01, 2008

AutoHotKey

AutoHotKey is an excellent which can customize keyboard/mouse shortcuts. It provides a simple script language which can be used to write scripts describing keyboard/mouse shortcuts. I am surprised that AutoHotKey provides abundant functionality which can be used in your scripts, e.g. get a file from a URL address, read/write file, control all aspects of window...

Besides, there are some available scripts on line which implement some common requirements, like window drag...
You can see some good ones here. If you want more, this forum includes thousands of scripts.

Among them, I found Niftywindow is a free one which implements lots of new functionality. So I downloaded it and modified the source file to fit in my need. It provides so many features so that some of them will not be used frequently. I chose some shortcuts which would be used by me in everyday using.

Saturday, May 03, 2008

Differences of array and pointer in C/C++

(1) Array

int main(){
    char *p = "hello,world";
    char *p2 = "hello,worldc";
    char str[] = "hello,world";
    char str2[] = "hello,world";    
    printf("%x %x %x %x\n", p, p2, str, str2);

}

Sample output(compiled with GCC 4.1.2 on Ubuntu 6.06.1LTS):

804856c 8048578 bf879674 bf879668

Conclusions:
<1> Strings pointed by pointers p and p2 are allocated in different places from strings of str and str2. The first and second strings are allocatd to static memory area and these two strings can not be modified. So the following statement makes the program crash(run time error instead of compilation time):
    p[0] = 'd'
The third and fourth strings are allocated on stack.  They can be modified. They just exist in current function call and after this function is executed they are discarded and should not be referenced any more.
<2> Pointers can be modified arbitrarily while array variables can not be modified.
Following code is illegal:

    char str[] = "hello,world";
    char str2[] = "abcdefgh";
    str = str2;    //illegal, can not be compiled successfully.
str++; //illegal
str+1; //legal, just like an ordinary addition.

In other words, array name is a reference to an array and it represents the array.  It is kind of a contant.
<3> Array name cannot be used a left value. Array name can be used as pointer if it acts as right value. Pointer can not be cast to array!!!
<4> Apply sizeof to array name, you will get size of the array. Apply sizeofi to a pointer, you will get size of the pointer(generally, four bytes).
<5> Array must be initialized and corresponding memory is allocated.
<6> void function(type array[]) is equivalent to void function(type *pointer).

数组要么在静态存储区被创建(如全局数组),要么在栈上被创建。数组名对应着(而不是指向)一块内存,其地址与容量在生命期内保持不变,只有数组的内容可以改变。
指针可以随时指向任意类型的内存块,它的特征是“可变”,所以我们常用指针来操作动态内存。指针远比数组灵活,但也更危险。
不能对数组名进行直接复制与比较。示例7-3-2中,若想把数组a的内容复制给数组b,不能用语句 b = a ,否则将产生编译错误。
当数组作为函数的参数进行传递时,该数组自动退化为同类型的指针.
对数组名sizeof()得到的是数组所占的大小,而对指针sizeof()得到的是指针内容所占的字节数。
数组定义时必须初始化并分配空间,而指针定义时可以不初始化。
如果将数组或指针作为自定义函数的形参,此时数组名和指针完全等价.在函数体内可以用数组名进行自增或赋值等运算,因为在一般的编译程序中,将函数的形参数组编译成指针.既f(char Data[])====>f(char *Data) 

Friday, April 11, 2008

Fiddler in Firefox

The only reason that sometimes I use IE is Fiddler which is an excellent HTTP analytic and debugging tool. By searching around, I found that it could be used with Firefox besides IE. The official document is here http://www.fiddlertool.com/Fiddler/help/hookup.asp.

For Firefox 1.x, you need to select Tools -> Options -> General -> Connection Settings, then a pop-up windows is displayed. You select "Automatic proxy configuration URL" and input "C:\Documents and Settings\gerald\My Documents\Fiddler2\Scripts\BrowserPAC.js" in my case. You should modify the path accordingly. That file BrowserPAC.js is created and maintained by Fiddler which contains proxy information. Every time Fiddler is started or shutdown, that file is modified to reflect the state of Fiddler.

When Fiddler is started, the file is changed to:

function FindProxyForURL(url, host){
  return 'PROXY 127.0.0.1:8888';
}

From the content, you can guess that, actually Fiddler works as a proxy server which listens at port 8888. You can make any HTTP application which supports proxy setting redirect traffic to Fiddler.
Of course, you can set "Manual proxy configuration" for Firefox and type proxy address 127.0.0.1:8888. However, every time you want to  or don't want to use Fiddler, you must change the proxy setting. It is not convenient.

When Fiddler is shut down, the file is changed to:

function FindProxyForURL(url, host){
  return 'DIRECT';
}

That looks great, right? But there is a pitfall. Firefox does not detect change of the proxy configuration file. It means when you start or shut down Fiddler, Firefox will not be able to detect that. You must force Firefox to reload the proxy file:
 image

Saturday, March 29, 2008

Java XML related concepts and implementations

Revolving around XML processing, there are many concepts and corresponding Java libraries/packages. For a beginner, he/she may get confused with bunch of different concepts and libraries. I aim to record what I have learnt about xml processing.

(1) The first aspect is how to parse and process xml documents. Following is a list of current popular xml processing interface.

(1.1) DOM (Document Object Model)
This is a model standardized by W3C (http://www.w3.org/DOM/DOMTR). It is a suite which contains three levels. DOM is platform- and language-neutral interface by which users can manipulate xml document. For example, users can retrieve the root element of a xml document and get its child nodes and attributes.
Generally, implementation of DOM is to build a tree structure in memory. It means that only when the whole document is gained by the parser can the parser build dom tree. The obvious drawback is its performance considering a large xml document. What's more, sometimes the user just wants to get a small part of a large xml document. In this case, to build a in-memory tree is not a good solution.
(1.2)SAX (Simple API for XML)
    This interface does not come from formal organization. At first, it was a Java implementation of XML parser with some different concepts from DOM. After it was published, it gradually was accepted by industry. Finally, SAX becomes a set of interfaces which define a new way to parse XML document. It is language independent. Currently, SAX are implemented in many programming languages.
    " SAX is a streaming interface — applications receive information from XML documents in a continuous stream, with no backtracking or navigation allowed. This approach makes SAX extremely efficient, handing XML documents of nearly any size in linear time and near-constant memory, but it also places greater demands on the software developer's skills."
    SAX is the best known example of Event-based APIs. "An event-based API, on the other hand, reports parsing events (such as the start and end of elements) directly to the application through callbacks, and does not usually build an internal tree. The application implements handlers to deal with the different events, much like handling events in a graphical user interface."
    This mechanism requires programmers to understand concept similar to state-machine. Programmers should maintain a state machine conversion of different states is based on events SAX generates.
(1.3)XPP (XML Pull Parser)
    SAX is push-based interface. It means SAX reads XML document from a stream and invokes corresponding event handlers you have registered. However, this is still inefficient considering sometimes users just want to handle a small part of xml document. In this case, XPP is better. XPP manipulates corresponding elements/attributes based on users' requests. As a result, only parts of xml document are processed with other parts untouched.
(1.4)StAX (Streaming API for XMl)
    Excerpt from http://en.wikipedia.org/wiki/StAX:
    "These two access metaphors can be thought of as polar opposites. A tree based API allows unlimited, random, access and manipulation, while an event based API is a 'one shot' pass through the source document.

StAX was designed as a median between these two opposites. In the StAX metaphor, the programmatic entry point is a cursor that represents a point within the document. The application moves the cursor forward - 'pulling' the information from the parser as it needs. This is different from an event based API - such as SAX - which 'pushes' data to the application - requiring the application to maintain state between events as necessary to keep track of location within the document."   

    Here(http://www.xml.com/pub/a/2001/11/14/dom-sax.html?page=1) is a good discussion about DOM and SAX in high level other than detailed processing details.

(2) Validation of XML document.
(2.1)DOM
    Validation of XML document based on W3C Schema or DTD is straightforward by using DOM. DOM maintains a global structure of processed xml document so that all information of the document can be retrieved easily without much effort.
(2.2)SAX
    Validation in SAX requires more effort. The reason is that SAX does not maintain information about full document. Certain kinds of validation require to access information of document in full. For example, DTD IDREF attribute is used to refer to other elements defined in document. It requires there exists an element in the document that uses that string as an ID attribute. So, to work around this issue, one needs to maintain every encountered ID attribute and IDREF attribute. In addition, some kinds of XML document processing require to access whole document (i.e. XPath). In this case, to build a document tree is better, which actually is included in DOM.
(2.3)XPP
    The same kind of thought applies here as well. To validate a XML document, XPP needs more workaround than DOM and SAX.
(3) Java-specific XML processing interface
     To make Java programmers access various xml processing interfaces in easier way, various Java-specific interfaces have been defined. This can make XML processing code parser independent. In other words, user can switch between different parsers if these parsers comply with the same set of interface definition. This is a good thing because the code is not bound to a specific parser.
(2.1) JAXP
    This is constructed by JCP. It provides a set of interfaces by which Java programmers are able to process xml documents. It contains DOM, SAX, XSLT, XInclude, XPath and XML validation. Actually, by putting JAXP into JDK, it increases its chance of being accepted more and more. Users don't need to install additional packages to make use of JAXP. This makes it convenient to use and makes configuration of run time environment more easily. To process XML documents, JDK is all you need. Of course, if you are not satisfied with performance of built-in parser shipped with JDK, you can download and install other parsers. This increases flexibility for sure.
(2.1) dom4j
    I think this interface is an extension of JAXP. It means dom4j is compliant with JAXP but it provides more extra functionalities. It seems that dom4j is more convenient for Java programmers to work with.
(2.1) JDOM
    This also shares the same goal with other interfaces described above. If I don't remember something wrong, it came out before JAXP. By using many Java language specific features, it can ease processing of XML document in Java. However, I don't think currently it is still popular considering standard JAXP and JAXP extension dom4j.
(4) Parsers
    Now it is time to introduce some XML parsers which actually process XML documents. Two popular open source parses I know are Xerces and Crimson. You can google and find comparison between these two parsers. Basically, both of them support parsing by DOM and SAX.
(5) Relationship between these terms.
    To eliminate possible confusion, some clarification of relationship among these terms may be necessary.
    For APIs/Interfaces, they define implementation independent specification of XMl manipulation. They are just sets of interfaces and not implementations. Implementation of these interfaces are generally based on parsers which do the actual work. The implementation consists of a slim layer sitting on top of functionalities of parsers. It wraps functionalities of underlying parsers (may be Xerces, Crimson) to provide a unified interface which is defined by corresponding specification (JAXP or dom4j...). So generally after you download a library from website of dom4j or JDOM or JAXP, the library contains Xerces/dom4j lib. You can figure it out if you browse directory layout of the dom4j/JDOM/JAXP library.
(6) Further work
    Although JAXP/dom4j/JDOM eases processing of XML document, it is far from what Java programmers expect. To work on DOM level is still a clumsy job. You have to retrieve an element or attribute so that you can manipulate its content. Generally, programmers need to know details of the XML documents.
    One advanced idea of XML processing is to build correspondence between Java classes and XML Schema(may be DTD) and correspondence between Java objects and XML document. Then programmers just need to manipulate those Java objects instead of elements/attributes in XML documents.
    There are many libraries which implement binding between XMl and Java, e.g. JIBX, XMLBeans, ADB. JCP constructed a standard called JAXB (Java API for XML Binding?). Glassfish provides a reference implementation. JAXME is also an implementation of JAXB. However JAXME have not published a new version since 2006. So I don't know whether it is still developed actively.
    However, because correspondence between these two parts sometimes is not so natural, it may increase burden of programmers. Sometimes, the correspondence does not comply with what programmers expect. In this case, some human intervention is necessary. Then programmers must understand details of rules used during conversion. There may be many rules so that it is not a trivial task to grasp them all. Sometimes you don't need to understand them all. However, to figure out which rule you should customize is still not an easy task. As a result, some programmers prefer to manipulate XML using DOM/SAX instead of XML-Java binding.
(7) Related area
    To illustrate usage of XML related technologies, one inevitable area is Web Service.
    Some useful projects which ease of development of web service in Java are created. Here(http://wiki.apache.org/ws/StackComparison) is a list of frameworks and some stack comparison is presented as well. One additional lib which is not mentioned in that article is XINS(http://xins.sourceforge.net/). For web service client, WSIF is a client framework which can make web service client be composed easily. It is donated by IBM to Apache foundation. However, it seems not to be actively developed now. I am not sure.
    Every time variation of Java technologies exist, JCP is willing to standardize it. The web service area is not exceptional. Firstly, it proposed JAX-RPC specification. It standardized web service development based on WSDL/SOAP. One part inside the specification is binding between Java classes/objects and WSDL. In JAX-RPC specification, it contains detailed binding rules. After some time, JAX-RPC evolved into 2.0 and it is renamed to JAX-WS 2.0. The reason may be WS is a buzzword in industry and it may better capture the intent of the specification. In JAX-WS 2.0, WSDL-Java binding is delegated to JAXB. I remember JAX-WS implementation is included in JDK 6. JAX-RPC/JAX-WS specification describes interface by which programmers can easily build web service client and server programs. Besides, programmers can customized handling of transmissioned messages(may be SOAP) by plugging in handlers.

Sunday, March 16, 2008

Java Bytecode

When javac is used to compile a Java program, bytecode(.class files) is generated which can be run on JVM.
Command javap can be used to display content of .class files in a meaningful way.
    javap ClassName        //print out field/method definition.
    javap -c ClassName    //print out all disassembled code.

A simple introduction:
http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/

JVM specification:
http://java.sun.com/docs/books/jvms/

Java Reflection and bytecode ...

I found a series of very useful articles about Java. Here is the address:http://www.ibm.com/developerworks/java/library/j-dyn0429/.
In this series, the author introduces classloading, bytecode, reflection, Class-transformation on-the-fly...

Javassist --- manipulation of bytecode
Introduction from official site:
"Javassist (Java programming assistant) is a load-time reflective system for Java. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file before the JVM loads it. Unlike other similar systems, Javassist provides source-level abstraction; programmers can modify a class file without detailed knowledge of the Java bytecode. They do not have to even write an inserted bytecode sequence; Javassist instead can compile a fragment of source text on line (for example, just a single statement). This ease of use is a unique feature of Javassit against other tools."
http://labs.jboss.com/javassist/
http://www.csg.is.titech.ac.jp/~chiba/javassist/

Configuration Of Tomcat

(Part of this post is cited from Tomcat Offcial site)
    I have been using Tomcat as JSP/Servlet container in current project. However, I have not got full understanding about Tomcat configuration. Every time I need to modify configuration, I search on the web and learn those related elements. I don't have a big picture about it. To fully understand the configuration, I read through official document at this site http://tomcat.apache.org/tomcat-6.0-doc/config/. And as I expect, it helps me a lot.
The configuration file of Tomcat is $TOMCAT_HOME/conf/server.xml.
(1) Server

Root element is server.
(2) Service
    A Service element represents the combination of one or more Connector components that share a single Engine component for processing incoming requests. So it looks like a container to enclose related Connector and Engine.
(3) Connector
    Connector receives requests from clients. There are two kinds of connector:HTTP and AJP.
HTTP Connector enables Tomcat to function as a stand-alone web server. An instance of connector listens for connections on a specific port number. One service can contain more than one connector. Normally, every connector maintains its own thread pool. However, this can be altered by setting property executor. If so, the connector will use the executor and all thread related properties are ignored. One advantage of using executor is that it can shared between components.
    "Each incoming request requires a thread for the duration of that request. If more simultaneous requests are received than can be handled by the currently available request processing threads, additional threads will be created up to the configured maximum (the value of the maxThreads attribute). If still more simultaneous requests are received, they are stacked up inside the server socket created by the Connector, up to the configured maximum (the value of the acceptCount attribute. Any further simultaneous requests will receive "connection refused" errors, until resources are available to process them."
    If SSL on HTTP is needed, corresponding connector element should be modified. Property redirectPort can be used to redirect SSL request received by a plain connector to another connector which supports SSL.
(4) Engine
    Following all connector elements in a service component is engine element. Engine element represents processing machinery associated with a service. It gets and processes all requests from connectors and returns the response to Connector. Note: exactly one engine element should be defined inside a service element.
(5) Host
    Inside engine element, multiple host elements can be nested. Each host element represents a virtual host. At least one host is required, and one of the hosts MUST match value of property defaultHost of engine element. Element alias can be used to give alias. Property appBase is used to specify application base directory of the virtual host. Properties deployOnStartUp and autoDeploy can be configured to set functionality of auto-deployment.
(6) Context
Context element represents a web application which is run inside a certain virtual host. "Each web application is based on a Web Application Archive (WAR) file, or a corresponding directory containing the corresponding unpacked contents, as described in the Servlet Specification (version 2.2 or later)." However, in high version of Tomcat, Context elements do not need to written in server.xml. Detailed information is:
    "For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file. This is because it makes modifing the Context configuration more invasive since the main conf/server.xml file cannot be reloaded without restarting Tomcat.
Context elements may be explicitly defined:
  • in the $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps
  • in the $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host
  • in individual files (with a ".xml" extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. context#path.xml. The default web application may be defined by using a file called ROOT.xml.
  • if the previous file was not found for this application, in an individual file at /META-INF/context.xml inside the application files
  • inside a Host element in the main conf/server.xml

How to map incoming request to specific web app?
    "The web application used to process each HTTP request is selected by Catalina based on matching the longest possible prefix of the Request URI against the context path of each defined Context. Once selected, that Context will select an appropriate servlet to process the incoming request, according to the servlet mappings defined in the web application deployment descriptor file (which MUST be located at /WEB-INF/web.xml within the web app's directory hierarchy)."

Deployment
    If you want to develop and deploy your JSP/Servlet applications, consult this page: http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html. It contains information about: Standard directory layout, where to put shared libraries, web.xml and deployment. This link also contains some useful information: http://tomcat.apache.org/tomcat-6.0-doc/config/host.html#Automatic%20Application%20Deployment.

Configuration of virtual host:
http://hi.baidu.com/zouziting/blog/item/7e3ad7808f3491d79123d936.html