Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between server.xml, context.xml and web.xml in the conf directory of Tomcat 7.0.4

 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

There are 3 xml files - server.xml, context.xml and web.xml in the conf directory of Tomcat.
What is the difference between them.

Also we write a web.xml for each web application and it resides in the WEB-INF directory.

Just today, i wanted to use Connection Pooling in Eclipse, and i read on the internet that we could add context.xml file in the META-INF directory of the WebContent folder in an application in Eclipse.
In this context.xml file we could specify the DataSource details in the Resource element.


Could anybody please explain the difference between all the xml files.

And also if i have to implement the connection pooling, then in which xml file should i enter the details.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17987
47
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start with, every J2EE webapp has 2 deployment configuration files: the server-independent Deployment Descriptor (web.xml) and the server-dependent Deployment Descriptor (context.xml). More on this in a minute.

The Tomcat server.xml file is the configuration of the Tomcat server itself. Tomcat is actually a series of components that are wired together to create a server by a master Tomcat JavaBean that reads (digests) the server.xml file in order to know what components to include, how to configure them, and how to interconnect them to create a working server. The server.xml file can contain server-dependent deployment descriptors as sub-elements, although that has been discouraged for a long, long time now. Among other reasons, because it required restarting the entire Tomcat server to modify a single webapp.

The server-dependent deployment descriptors can reside in a directory of their own (conf/Catalina/localhost) or be embedded within a webapp's WAR (META-INF/context.xml). Incidentally, the "localhost" in the tomcat Catalina directory is merely the default. For Tomcat servers where a single Tomcat runs multiple hosts, there is a separate config directory under Catalina for each host.

So you have server.xml for Tomcat itself and a context.xml/web.xml pair for each webapp deployed in tomcat. The context.xml and web.xml in the Tomcat conf directory are used to guide the default behavior of Tomcat. For example, when you submit a URL which resolves to a static resource such as an image, Tomcat doesn't simply "know" how to convert that image URL into a response stream containing that image, it determines that the URL doesn't match any of the patterns defined in the web.xml for the webapp context ID of that URL and bounces the request up to the default environment where the Tomcat DefaultServlet takes care of the actual details of converting the URL to a resource location, opening the resource (image file) and transmitting the resource to the client. All of that is under the control of the master context and web-inf config files in the Tomcat conf directory.

Certain resources can be defined per-application or shared between multiple applications. The most common ones are security Realms and Connection Pools. When you want a per-application definition of one of those resources, you'd do it in the Context xml file. When you want it common between apps, you'd define it in the server.xml, since it's the only common configuration between all webapps. There are precedence rules when 2 definitions conflict, but those rules are enumerated in the Tomcat docs.
 
Rohit W. Tawde
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Thank you so much for such a detailed explanation.

Really. Thank you so much.

Cheers,
Rohit
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like your answer, but I think it could be a little bit clearer. The word "server" is overloaded. Instead I think it would be better to say "tomcat instance", or "tomcat server instance", or even "tomcat http server instance". Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic