<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; Apache Tomcat</title>
	<atom:link href="http://www.javabeat.net/category/web-servers/apache-tomcat/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Fri, 24 May 2013 01:32:07 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Tomcat Interview Questions</title>
		<link>http://www.javabeat.net/2010/08/tomcat-interview-questions/</link>
		<comments>http://www.javabeat.net/2010/08/tomcat-interview-questions/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 00:42:03 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Apache Tomcat]]></category>
		<category><![CDATA[Interview Questions]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=457</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Tomcat Interview Questions &#8211; 1 How do you create multiple virtual hosts? If you want tomcat to accept requests for different hosts e.g., www.myhostname.com then you must 0. create ${catalina.home}/www/appBase , ${catalina.home}/www/deploy, and ${catalina.home}/conf/Catalina/www.myhostname.com 1. add a host entry in the server.xml file 1 &#60;Host appBase=&#34;www/appBase&#34; name=&#34;www.myhostname.com&#34;/&#62; 2. Create the the following file under conf/Catalina/www.myhostname.com/ROOT.xml [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><h2>Tomcat Interview Questions &#8211; 1</h2>
<h3>How do you create multiple virtual hosts?</h3>
<p>If you want tomcat to accept requests for different hosts e.g., www.myhostname.com then you must 0. create ${catalina.home}/www/appBase , ${catalina.home}/www/deploy, and ${catalina.home}/conf/Catalina/www.myhostname.com</p>
<p>1. add a host entry in the server.xml file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">  &lt;Host appBase=&quot;www/appBase&quot; name=&quot;www.myhostname.com&quot;/&gt;</pre></td></tr></table></div>

<p>2. Create the the following file under conf/Catalina/www.myhostname.com/ROOT.xml</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Context
    path=&quot;/&quot;
    docBase=&quot;www/deploy/mywebapp.war&quot;
    reloadable=&quot;true&quot; antiJARLocking=&quot;true&quot;&gt;
&lt;/Context&gt;</pre></td></tr></table></div>

<p>Add any parameters specific to this hosts webapp to this context file</p>
<p>3. put your war file in ${catalina.home}/www/deploy</p>
<p>When tomcat starts, it finds the host entry, then looks for any context files and will start any apps with a context</p>
<p>To add more sites just repeat and rinse, all webapps can share the same war file location and appbase</p>
<h3>How will you load properties file?</h3>
<p>* Use a ResourceBundle. See the Java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes directory or in a jar file contained in the WEB-INF/lib directory.</p>
<p>* Another way is to use the method getResourceAsStream() from the ServletContext class. This allows you update the file without having to reload the webapp as required by the first method. Here is an example code snippet, without any error trapping:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">// Assuming you are in a Servlet extending HttpServlet
                    // This will look for a file called &quot;/more/cowbell.properties&quot; relative
                    // to your servlet Root Context
                   InputStream is = getServletContext().getResourceAsStream(&quot;/more/cowbell.properties&quot;);
                   Properties  p  = new Properties();
                   p.load(is);
                 is.close();</pre></td></tr></table></div>

<h3>Can I set Java system properties differently for each webapp?</h3>
<p>No. If you can edit Tomcat&#8217;s startup scripts, you can add &#8220;-D&#8221; options to Java. But there is no way to add such properties in web.xml or the webapp&#8217;s context.</p>
<h3>How do I configure Tomcat to work with IIS and NTLM?</h3>
<p>Follow the standard instructions for when the isapi_redirector.dll Configure IIS to use &#8220;integrated windows security&#8221;</p>
<p>In server.xml, make sure you disable tomcat authentication:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;Connector port=&quot;8009&quot; enableLookups=&quot;false&quot; redirectPort=&quot;8443&quot; protocol=&quot;AJP/1.3&quot; tomcatAuthentication=&quot;false&quot; /&gt;</pre></td></tr></table></div>

<h3>How can I access members of a custom Realm or Principal?</h3>
<p>When you create a custom subclass of RealmBase or GenericPrincipal and attempt to use those classes in your webapp code, you&#8217;ll probably have problems with ClassCastException. This is because the instance returned by request.getUserPrincipal() is of a class loaded by the server&#8217;s classloader, and you are trying to access it through you webapp&#8217;s classloader. While the classes maybe otherwise exactly the same, different (sibling) classloaders makes them different classes.</p>
<p>This assumes you created a My&#8220;Principal class, and put in Tomcat&#8217;s server/classes (or lib) directory, as well as in your webapp&#8217;s webinf/classes (or lib) directory. Normally, you would put custom realm and principal classes in the server directory because they depend on other classes there.</p>
<p>Here&#8217;s what you would like to do, but it throws ClassCastException:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">MyPrincipal p = request.getUserPrincipal();
String emailAddress = p.getEmailAddress();</pre></td></tr></table></div>

<p>Here are 4 ways you might get around the classloader boundary:</p>
<p>1) Reflection</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">Principal p = request.getUserPrincipal();
String emailAddress = p.getClass().getMethod(&quot;getEmailAddress&quot;, null).invoke(p, null);</pre></td></tr></table></div>

<p>2) Move classes to a common classloader</p>
<p>You could put your custom classes in a classloader that is common to both the server and your webapp &#8211; e.g., either the &#8220;common&#8221; or bootstrap classloaders. To do this, however, you would also need to move the classes that your custom classes depend on up to the common classloader, and that seems like a bad idea, because there a many of them and they a core server classes.</p>
<p>3) Common Interfaces</p>
<p>Rather than move the implementing custom classes up, you could define interfaces for your customs classes, and put the interfaces in the common directory. You&#8217;re code would look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public interface MyPrincipalInterface extends java.security.Principal {
  public String getEmailAddress();
}
&nbsp;
public class MyPrincipal implements MyPrincipalInterface {
...
  public String getEmailAddress() {
    return emailAddress;
  }
}
&nbsp;
public class MyServlet implements Servlet {
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    MyPrincipalInterface p = (MyPrincipalInterface)request.getUserPrincipal();
    String emailAddress = p.getEmailAddress();
...
}</pre></td></tr></table></div>

<p>Notice that this method gives you pretty much the webapp code you wanted in the first place</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>4) Serializing / Deserializing</p>
<p>You might want to try serializing the response of &#8216;request.getUserPrincipal()&#8217; and deserialize it to an instance of [webapp]MyPrincipal.</p>
<h3>How do I override the default home page loaded by Tomcat?</h3>
<p>After successfully installing Tomcat, you usually test it by loading http://localhost:8080 . The contents of that page are compiled into the index_jsp servlet. The page even warns against modifying the index.jsp files for this reason. Luckily, it is quite easy to override that page. Inside $TOMCAT_HOME/conf/web.xml there is a section called &lt;welcome-file-list&gt; and it looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;welcome-file-list&gt;
        &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
        &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
        &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;</pre></td></tr></table></div>

<p>The default servlet attempts to load the index.* files in the order listed. You may easily override the index.jsp file by creating an index.html file at $TOMCAT_HOME/webapps/ROOT. It&#8217;s somewhat common for that file to contain a new static home page or a redirect to a servlet&#8217;s main page. A redirect would look<br />
like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;html&gt;
&nbsp;
&lt;head&gt;
&lt;meta http-equiv=&quot;refresh&quot; content=&quot;0;URL=http://mydomain.com/some/path/to/servlet/homepage/&quot;&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;/body&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>This change takes effect immediately and does not require a restart of Tomcat.</p>
<h3>How do I enable Server Side Includes (SSI)?</h3>
<p>Two things have to be done for tomcat to aknowledge SSI scripts:</p>
<p>1. Rename $CATALINA_BASE/server/lib/servlets-ssi.renametojar to $CATALINA_BASE/server/lib/servlets-ssi.jar.</p>
<p>2. Uncomment the section of web.xml found in $CATALINA_BASE/conf/web.xml that deals with SSI. it looks like this when it is uncommented:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;servlet&gt;
        &lt;servlet-name&gt;ssi&lt;/servlet-name&gt;
        &lt;servlet-class&gt;
          org.apache.catalina.ssi.SSIServlet
        &lt;/servlet-class&gt;
        &lt;init-param&gt;
          &lt;param-name&gt;buffered&lt;/param-name&gt;
          &lt;param-value&gt;1&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;init-param&gt;
          &lt;param-name&gt;debug&lt;/param-name&gt;
          &lt;param-value&gt;0&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;init-param&gt;
          &lt;param-name&gt;expires&lt;/param-name&gt;
          &lt;param-value&gt;666&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;init-param&gt;
          &lt;param-name&gt;isVirtualWebappRelative&lt;/param-name&gt;
          &lt;param-value&gt;0&lt;/param-value&gt;
        &lt;/init-param&gt;
        &lt;load-on-startup&gt;4&lt;/load-on-startup&gt;
    &lt;/servlet&gt;</pre></td></tr></table></div>

<h3>How do I use DataSources with Tomcat?</h3>
<p>When developing J2EE web applications, the task of database connection management can be daunting. Best practice involves using a J2EE DataSource to provide connection pooling, but configuring DataSources in web application servers and connecting your application to them is often a cumbersome process and poorly documented.</p>
<p>The usual procedure requires the application developer to set up a DataSource in the web application server, specifying the driver class, JDBC URL (connect string), username, password, and various pooling options. Then, the developer must reference the DataSource in his application&#8217;s web.xml configuration file, and then access it properly in his servlet or JSP. Particularly during<br />
development, setting all of this up is tedious and error-prone.</p>
<p>With Tomcat 5.5, the process is vastly simplified. Tomcat allows you to configure DataSources for your J2EE web application in a context.xml file that is stored in your web application project. You don&#8217;t have to mess with configuring the DataSource separately in the Tomcat server.xml, or referencing it in your application&#8217;s web.xml file. Here&#8217;s how:</p>
<p>Install the JDBC Driver</p>
<p>Install the .jar file(s) containing the JDBC driver in Tomcat&#8217;s common/lib folder. You do not need to put them in your application&#8217;s WEB-INF/lib folder. When working with J2EE DataSources, the web application server manages connections for your application.</p>
<p>Create META-INF/context.xml</p>
<p>In the root of your web app directory structure, create a folder named META-INF (all caps). Inside that folder, create a file named context.xml that contains a Resource like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&nbsp;
&lt;Context&gt;
&nbsp;
  &lt;Resource name=&quot;jdbc/WallyDB&quot; auth=&quot;Container&quot;
            type=&quot;javax.sql.DataSource&quot; username=&quot;wally&quot; password=&quot;wally&quot;
            driverClassName=&quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;
            url=&quot;jdbc:sqlserver://localhost;DatabaseName=mytest;SelectMethod=cursor;&quot;
            maxActive=&quot;8&quot;
            /&gt;
&nbsp;
&lt;/Context&gt;</pre></td></tr></table></div>

<p>This example shows how to configure a DataSource for a SQL Server database named mytest located on the development machine. Simply edit the Resource name, driverClassName, username, password, and url to provide values appropriate for your JDBC driver.</p>
<p>Access the DataSource in Your Application</p>
<p>From a Servlet</p>
<p>Here&#8217;s how you might access the data in a servlet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">InitialContext ic = new InitialContext();
  DataSource ds = (DataSource) ic.lookup(&quot;java:comp/env/jdbc/WallyDB&quot;);
  Connection c = ds.getConnection();
  ...
  c.close();</pre></td></tr></table></div>

<p>Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with java:comp/env/</p>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fcategory%2Fweb-servers%2Fapache-tomcat%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/category/web-servers/apache-tomcat/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/category/web-servers/apache-tomcat/feed/" data-count="vertical" data-text="Apache Tomcat" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/08/tomcat-interview-questions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tomcat 6.0 Servlet Container Overview</title>
		<link>http://www.javabeat.net/2010/06/tomcat-6-0-servlet-container-overview/</link>
		<comments>http://www.javabeat.net/2010/06/tomcat-6-0-servlet-container-overview/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 00:10:52 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Apache Tomcat]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=1888</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>Tomcat 6.0 Developer&#8217;s Guide Current books on Tomcat are primarily focused on the application deployer or administrator. As a result, they invariably focus on the issues related to managing a Tomcat installation, configuring the runtime environment, and on deploying web applications. On the other hand, while books on servlet programming are targeted at Java web [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><H1><CENTER>Tomcat 6.0 Developer&#8217;s Guide</CENTER></H1><br />
<P>Current books on Tomcat are primarily focused on the application deployer<br />
or administrator. As a result, they invariably focus on the issues related to<br />
managing a Tomcat installation, configuring the runtime environment, and on<br />
deploying web applications.</P><br />
<P>On the other hand, while books on servlet programming are targeted at Java web<br />
developers, they often provide a container-agnostic view of the servlet specification.<br />
Tomcat is often a bit player in these books and has very few speaking lines.</P><br />
<P>This book fills the void between these two approaches.</P><br />
<P>It will take you on a guided tour of a living implementation of an industrial-strength<br />
servlet container.</P><br />
<P>Along the way, you will learn how various elements of the Servlet 2.5 specification as<br />
well as how the HTTP RFCs are implemented.</P><br />
<P>By the end of your journey, you will have acquired specialist grade skills in a range of<br />
technologies that contribute to the arena of Java server-side development.</P><br />
<P>This book intended to provide Tomcat administrators, deployers, and developers an<br />
introduction into the internal workings of the Tomcat servlet container.</P><br />
<P>At the same time, it provides Java web programmers with a deep appreciation of the<br />
Servlet APiby exploring its reference implementation—the Tomcat container.</P><br />
<P>While this book provides you with the conceptual background of all that is necessary to<br />
take your skills to the next level, it assumes that the reader has a general understanding of<br />
the Java programming language and Java web programming.</P><br />
<H1>What This Book Covers</H1><br />
<P><I>Chapter 1—Introduction to Tomcat</I> introduces you to the Tomcat container and provides<br />
you with the tools necessary to begin to take it apart. The key objective of this chapter is<br />
to allow you to make a current source distribution of Tomcat active in a development<br />
environment (Eclipse Galileo) so that you can trace the path that a request takes through<br />
the container&#8217;s code.</P><br />
<P><I>Chapter 2—Servlet APiOverview</I> provides the prerequisite information necessary to<br />
navigate the remainder of the book. It describes the Java Enterprise Edition Platform, the<br />
HTTP protocol, and the Servlet API, and serves as a refresher for those who are already<br />
familiar with Java EE web development.</P><br />
<P><I>Chapter 3—Servlet Container</I> Overview introduces the reader to the Tomcat container.<br />
This is the 10,000 foot overview of the container that provides a backdrop to the chapters<br />
that follow. All the components of Tomcat are described with just enough detail, so as<br />
not to overwhelm the reader with too much information, too early in the process.</P><br />
<P><I>Chapter 4—Starting up Tomcat</I> takes a closer look at the startup process for Tomcat. This<br />
is also where you will be first introduced to the Apache Digester project—a key<br />
component that we will revisit in later chapters. The chapter ends with an example that<br />
demonstrates how a web application can be deployed to a dissected Tomcat container<br />
living within an Integrated Development Environment.</P><br />
<P><I>Chapter 5—The Server and Service Components</I> discusses the Server component and<br />
investigates one of its key services—an implementation of the <B>Java Naming and<br />
Directory Interface (JNDI)</B> API. We are also introduced to the Lifecycle interface<br />
that almost every component within Tomcat implements in order to participate in a<br />
standardized event based listener mechanism. To show JNDiin action, our example<br />
considers connecting to a MySQL database to retrieve data.</P><br />
<P><I>Chapter 6—The Connector Component</I> introduces our first Tomcat luminary, the Coyote<br />
Connector. We take a closer look at the standard Java I/O implementation of an HTTP<br />
connector. In this chapter, we get a closer look at socket programming, advanced<br />
elements of the HTTP protocol, and the internals of the request processing mechanism.</P><br />
<P><I>Chapter 7—The Engine Component</I> describes the first request processing &#8216;Container&#8217;<br />
within Tomcat and gives us an inkling of things to come. We are also introduced to the<br />
Pipeline and its Valves, which are the standard request processing mechanism for<br />
Tomcat components.</P><br />
<P><I>Chapter 8—The Host Component</I> discusses the Tomcat implementation of a Virtual Host.<br />
This is the key component responsible for the deployment of web application contexts, as<br />
well as for the error page mechanism.</P><br />
<P><I>Chapter 9—The Context Component</I> is at the central core of this book. You get an upclose-<br />
and-personal look at how a Context is configured, how it accesses its resources,<br />
and how it implements its class loading magic.</P><br />
<P><I>Chapter 10—The Wrapper Component</I> takes us to the workhorse of the Tomcat<br />
component hierarchy. This component wraps an actual servlet, and as a result is close<br />
to a web developer&#8217;s heart. In addition to reviewing the mapping rules dictated by the<br />
Servlet API, we also look at the implementation of servlet filters and the request<br />
dispatcher mechanism.</P><br />
<P><I>Chapter 11—The Session Component</I> discusses how sessions are implemented in Tomcat<br />
to enable stateful behavior over the stateless HTTP protocol. In addition to looking at<br />
some core concepts, such as Java serialization and entropy gathering for random number<br />
generation, we look at the standard memory based session implementation, as well as an<br />
implementation that uses files to persist sessions.</P><br />
<H1><CENTER>Servlet Container Overview</CENTER></H1><br />
<P>In the last chapter, we noted that the Java Enterprise Edition can be considered to be<br />
nothing more than a set of specifications, or interfaces, for which service providers<br />
are required to provide implementations.</P><br />
<P>While it is the actual implementation that does all the work, these specifications<br />
ensure that each implementation can assume that all its other collaborating<br />
pieces work as described by their interfaces. In theory, this allows complex<br />
software platforms (such as application servers) to be assembled from<br />
constituent implementations, each of which is sourced from a different vendor.</P><br />
<P>In practice, it is highly unlikely that you will interface an EJB container from<br />
WebSphere and a JMS implementation from WebLogic, with the Tomcat servlet<br />
container from the Apache foundation, but it is at least theoretically possible.</P><br />
<P>Note that the term &#8216;interface&#8217;, as it is used here, also encompasses abstract classes.<br />
The specification&#8217;s APimight provide a template implementation whose operations<br />
are defined in terms of some basic set of primitives that are kept abstract for the<br />
service provider to implement. For instance, in Chapter 2, we noted that the servlet<br />
hierarchy is made up of the Servlet interface, and the GenericServlet and<br />
HttpServlet abstract classes within the javax.servlet package.</P><br />
<P>A service provider is required to make available concrete implementations of<br />
these interfaces and abstract classes. For example, the HttpSession interface<br />
is implemented by Tomcat in the form of org.apache.catalina.session.<br />
StandardSession.</P><br />
<P>Let&#8217;s return to the image of the Tomcat container that we saw in Chapter 1.</P><br />
<P><CENTER><IMG SRC="images/2010/06/Tomcat6.0/1.jpg"/></CENTER></P><br />
<P>As was stated in Chapter 1, the objective of this book is to cover the primary request<br />
processing components that are present in this image. Advanced topics, such as<br />
clustering and security, are shown as shaded in this image and are not covered.</P><br />
<P>In this image, the &#8216;+&#8217; symbol after the Service, Host, Context, and Wrapper<br />
instances indicate that there can be one or more of these elements. For instance, a<br />
Service may have a single Engine, but an Engine can contain one or more Hosts.<br />
In addition, the whirling circle represents a pool of request processor threads.</P><br />
<P>In this chapter, we will fl y over the architecture of Tomcat from a 10,000-foot<br />
perspective taking in the sights as we go.</P><br />
<H1>Component taxonomy</H1><br />
<P>Tomcat&#8217;s architecture follows the construction of a Matrushka doll from Russia. In<br />
other words, it is all about containment where one entity contains another, and that<br />
entity in turn contains yet another.</P><br />
<P>In Tomcat, a &#8216;container&#8217; is a generic term that refers to any component that can<br />
contain another, such as a Server, Service, Engine, Host, or Context.</P><br />
<P>Of these, the Server and Service components are special containers, designated as<br />
<B>Top Level Elements</B> as they represent aspects of the running Tomcat instance. All<br />
the other Tomcat components are subordinate to these top level elements.</P><br />
<P>The Engine, Host, and Context components are officially termed <B>Containers</B>, and<br />
refer to components that process incoming requests and generate an appropriate<br />
outgoing response.</P><br />
<P><B>Nested Components</B> can be thought of as sub-elements that can be nested inside<br />
either Top Level Elements or other Containers to configure how they function.<br />
Examples of n ested components include the Valve, which represents a reusable unit<br />
of work; the Pipeline, which represents a chain of Valves strung together; and a<br />
Realm which helps set up container-managed security for a particular container.</P><br />
<P>Other nested components include the Loader which is used to enforce the<br />
specification&#8217;s guidelines for servlet class loading; th e Manager that supports session<br />
management for each web application; the R esources component that represents the<br />
web application&#8217;s static resources and a mechanism to access these resources; and<br />
the Listener th at allows you to insert custom processing at important points in a<br />
container&#8217;s life cycle, such as when a component is being started or stopped.</P><br />
<P><PRE><CODE><br />
	Not all nested components can be nested within every container.<br />
</CODE></PRE></P><br />
<P>A final major component, which falls into its own category, is the <B>Connector</B>. It<br />
represents the connection end point that an external client (such as a web browser)<br />
can use to connect to the Tomcat container.</P><br />
<P>Before we go on to examine these components, let&#8217;s take a quick look at how they are<br />
organized structurally.</P><br />
<P><CENTER><IMG SRC="images/2010/06/Tomcat6.0/2.jpg"/></CENTER></P><br />
<P>Note that this diagram only shows the key properties of each container.</P><br />
<P>When Tomcat is started, the <B>Java Virtual Machine (JVM)</B> instance in which it<br />
runs will contain a singleton Server top level element, which represents the entire<br />
Tomcat server. A Server will usually containjust one Service object, which is a<br />
structural element that combines one or more Connectors (for example, an HTTP<br />
and an HTTPS connector) that funnel incoming requests through to a single<br />
Catalina servlet Engine.</P><br />
<P>The Engine represents the core request processing code within Tomcat and supports<br />
the definition of multiple <B>Virtual Hosts</B> within it. A virtual host allows a single<br />
running Tomcat engine to make it seem to the outside world that there are multiple<br />
separate domains (for example, www.my-site.com and www.your-site.com) being<br />
hosted on a single machine.</P><br />
<P>Each virtual host can, in turn, support multiple web applications known as<br />
<B>Contexts</B> that are deployed to it. A context is represented using the web application<br />
format specified by the servlet specification, either as a single compressed <B>WAR<br />
(Web Application Archive)</B> file or as an uncompressed directory. In addition, a<br />
context is configured using a web.xml file, as defined by the servlet specification.</P><br />
<P>A context can, in turn, contain multiple servlets that are deployed into it, each of<br />
which is wrapped in a Wrapper component.</P><br />
<P>The Server, Service, Connector, Engine, Host, and Context elements that<br />
will be present in a particular running Tomcat instance are configured using the<br />
server.xml configuration file.</P><br />
<P><PRE><CODE><br />
	Things are a bit more complicated than this. However, we&#8217;ll defer the<br />
	complexity until later chapters when we deal with each component in a<br />
	lot more detail.<br />
</CODE></PRE></P><br />
<H1>Architectural benefits</H1><br />
<P>This architecture has a couple of useful features. It not only makes it easy to manage<br />
component life cycles (each component manages the life cycle notifications for its<br />
children), but also to dynamically assemble a running Tomcat server instance that<br />
is based on the information that has been read from configuration files at startup.<br />
In particular, the server.xml file is parsed at startup, and its contents are used to<br />
instantiate and configure the defined elements, which are then assembled into a<br />
running Tomcat instance.</P><br />
<P><PRE><CODE><br />
	The server.xml file is read only once, and edits to it will not be picked<br />
	up until Tomcat is restarted.<br />
</CODE></PRE></P><br />
<P>This architecture also eases the configuration burden by allowing child containers<br />
to inherit the configuration of their parent containers. For instance, a Realm defines<br />
a data store that can be used for authentication and authorization of users who<br />
are attempting to access protected resources within a web application. For ease of<br />
configuration, a realm that is defined for an engine applies to all its children hosts<br />
and contexts. At the same time, a particular child, such as a given context, may<br />
override its inherited realm by specifying its own realm to be used in place of its<br />
parent&#8217;s realm.</P></p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2010/06/tomcat-6-0-servlet-container-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
