Win a copy of Terraform in Action this week in the Cloud forum!

Budi Kurniawan

+ Follow
since Sep 15, 2004
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Budi Kurniawan

Originally posted by Chandrasekhar S:

In my application each time I change my servlet ,tomcat needs to be restarted.Can this be avoided?

I tried to set the parameter reloadable=�true�,but that seems not working.

<Context path=�/myapp� docBase=�webapps/examples� debug=�0� reloadable=�false�

Is it possible to reload the application with out restarting the server.Something similar to hot deployement in JBoss


The value of the docBase attribute does not seem to be correct to me. It's relative to the webapps directory, so it should be "example". also, reloadable must be true.
17 years ago
The WebappClassLoader does more than simply loading Java classes. For example, it has a background thread (in Tomcat 4) and uses the common background thread (Tomcat 5) to support automatic application reload, i.e. if the web.xml or any .jar file has changed.
17 years ago

Originally posted by Paul Erion:
Is it possible for a single instance of Tomcat to listen on multiple ports?



I think what Paul wanted to know was whether a protocol (HTTP or HTTPS) can listen on multiple ports. The answer is yes. In the server.xml file, find the following (in Tomcat 5):

<Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />

As you can see, by default Tomcat listens on port 8080. If, for example, you want Tomcat to listen on another port, say 8081, just add this below the previous Connector element:

<Connector port="8081"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />

The reason for this is this.
The Connector element represents a connector. Upon startup, Tomcat will parse the server.xml file and create objects based on the content of the file. A Connector element will cause Tomcat to create a Connector object. Another Connector element will give you another instance of Connector. With the above configuration, you have 2 connectors listening on port 8080 and 8081 respectively. You only have one container though. The connectors create a request and response object for each incoming HTTP request and pass it to the container.

You can read Chapter 3 of our How Tomcat Works book that discusses connector. This chapter is freely available from
17 years ago
For rapid development, thought, you might not want to stop and restart Tomcat. What you need is the feature that automatically reloads the application when one of the classes or the web.xml file changes. Here is how you can configure Tomcat for a given application:

<Context path="/myApp" docBase="myApp" debug="8" reloadable="true"/>

By default, the background thread checks any new/modified file every 15 seconds. That's too long for some people, including me. I normally like to make it 7. Here is how:

<Context path="/myApp" docBase="/myapp" debug="8" reloadable="true">
<Loader className="org.apache.catalina.loader.WebappLoader" reloadable="true" checkInterval="7" debug="7"/>

I've also written a loader for Struts development, which reloads the application if the struts-config.xml changes. You can download it from the book's website.
17 years ago
I've been designing and developing servlet/JSP applications for years now. And, naturally, if you like something, at one stage you will start asking questions about it. The questions that came to mind were:
1. What is it that prevents me from changing the value of a request parameter. I knew it was a Map, but how come I could only read it?
2. How does the servlet container know when to invalidate a session? There must be a background thread, but where?
3. How many instances does the container create for each servlet? What is the impact on performance?
4. How does the container parse the web.xml file?
5. How do the Manager and Admin application get access to Catalina so that they can start and stop, deploy and redeploy, an application?

Those sort of things.

So, I began to play with Tomcat code. It was an easy choice because it was popular and, more importantly, the source is available. The more I read it, the more I got interested. And, honestly, I learned a lot.

I decided to write How Tomcat Works to share what I have learned (the hard way) so that those who want to extend Tomcat (or even join the development team) do not have to go through what I did.
17 years ago
Tomcat 4 and 5 do swap session objects to secondary storage such as a database. You can configure Tomcat to do so. Internally, Tomcat employs a background thread that checks how old a session object is, whether a session needs to be invalidated, etc. The module that manages session objects is called Manager (discussed in Chapter 9: Session Management).
17 years ago
It doesn't offer configuration tips. Rather it teaches how to write your own components. It is not impossible to write a module that can improve performance.

For example, there are four types of containers: Engine, Host, Context, and Wrapper. Every HTTP request for a servlet/JSP page goes through these four. If you know that your Tomcat will only run one application, you can create a Context module that does not need a Host or an Engine. That way, the connector will pass all requests directly to the Context.
17 years ago
The book covers Tomcat 4 and 5. 5 is not a complete rewrite of 4. The main difference is that tomcat 5 supports Servlet 2.4 and JSP 2.0, while 4 supports Servlet 2.3 and JSP 1.2.

Internally, the servlet container in 5 is more efficient. For example, in 4 each component that needs background processing has its own thread. In 5 one thread is shared by all the components.

Some background processing tasks include:
1. invalidating expired session objects.
2. reloading an application whose web.xml or one of its classes has changed.


This is one of the topics "How Tomcat Works" discusses in detail.
17 years ago
Please try again. We did not encrypt the chapters.

17 years ago

Originally posted by Michael Cleary:
The review on Amazon indicated that this book was for advanced users. Could the authors be a little more specific about who the book is targeted at? Would it be understandable/useful for an "ordinary" user of Tomcat?


It's not easy to say. For example, Richard Monson-Haefel says in his blog on

"... You don't have to be rocket scientist to appreciate the book or understand it. If you have basic Java skills the book will be a little challenging but not too bad. If you have intermediate to advanced Java skills, you'll have no problem at all and you will probably learn a lot. ..."

When writing a chapter, Paul and I have tried to explain all relevant topics prior to developing a given module for that chapter. However, best is to download the 6 chapters from and see for yourself.
17 years ago

Originally posted by Ko Ko Naing:
Welcome to the Ranch, Budi and paul!!!

Congratulations to the first book from the self-publishing company, BrainySoftware, as well!!!

We hope to see more great book like "How Tomcat Works" in the future...

[ September 14, 2004: Message edited by: Ko Ko Naing ]

Thank you, I will also share my experience self publishing this book on JavaRanch in the near future.
17 years ago
Dear All,
Thanks for all the questions and comments. I'll reply to every one of them in a moment, but first I'd like to use this opportunity to write a brief introduction to this book.

"How Tomcat Works" explains the internal workings of Tomcat. It does not require you to be an expert in Tomcat to understand it. In fact, this book invites you to write Tomcat from scratch. How is it possible? Tomcat is so complex, you might say. That's true, but this book does not try to explain everything in one go.

Chapter 1 starts off with a discussion of Java sockets and shows how to use it to build a simple Java web server that can only display static pages. You know how to read files in Java, don't you? Well, then you can read (static) HTML files. The application is so simple that if you understand how sockets work (explained at the beginning of the chapter), you should be able to understand it. In fact, it only has 3 small classes.

Chapter 2 builds on the application developed in Chapter 1. The application in Chapter 2 is a modified version of the web server in Chapter 1. It adds one function so that the web server can also load and run a servlet. So now the web server has evolved into a servlet container. Because it is so primitive, it can only run very simple servlets. Of course this chapter also explains how the application works. If you have written a servlet before, you know that the javax.servlet.Servlet interface has a lifecycle method named service where you put the code to be executed when the servlet is invoked. This chapter teaches you how to load a servlet class, instantiate it and call its service method. Don't worry about the other 2 lifecycle methods (init and destroy) for now, they will be discussed in the next chapters.

Chapter 3 enhances the application in Chapter 2. You know that the Servlet interface's service method accepts 2 arguments as indicated by its signature:

public void service(HttpServletRequest request, HttpServletResponse response)

The application in Chapter 3 shows how to create the HttpServletRequest and HttpServletResponse objects for every HTTP request that invokes the servlet. (in chapter 2 the application calls the servlet's service method by passing null to its 2 arguments.) Now you see, the application is slightly smarter than the previous one.

And, after Chapter 20, you'll get Tomcat.

By now, I hope you understand the approach I used when writing this book and believe me that you do not need to be a Java expert to follow the book topics. Every Java feature that will be used in an application will be first explained in the corresponding chapter. For example, before discussing the use of JMX in Tomcat, the chapter explains what JMX is and how to use it. Before explaining how Digester is used to parse web.xml files, the chapter explains what Digester is and let you play with a simple application that uses Digester.

Now, is there any use of understanding how Tomcat works?

I received emails that asked me this question soon after Richard Monson-Haefel (the author of Enterprise Java Beans by O'Reilly) mentioned this book in his blog ( I would simply say yes.

First of all, by understanding how a servlet container works, you will be able to write better servlet/JSP applications. It will probably answer some of the questions you have been asking too, such as whether or not a container creates one instance or many instances of a servlet, how a servlet container manages sessions, how it creates request and response objects, why we cannot modify the request parameter values, etc.

Also, Tomcat is a real life Java application with such an elegant design. Knowing such an application was designed and built helps any Java programmers and architects. For example, this book shows how Tomcat uses a pool of request objects and at the same time guarantees thread safety. The book also teaches what JMX is and how to use it.

In addition, you will be able to customize Tomcat and build Tomcat components. To demonstrate this point, I have actually written a module for helping Struts programmers develop Struts applications more rapidly. The module reloads your Struts application when your struts-config.xml has changed. Better still, you only need 33 lines of code to write this module. That's right, writing Tomcat custom components can be very easy. You can download the module from the book's website.

Finally, I'd like to invite you to download 6 sample chapters from the book, on I hope you enjoy it.


[ September 15, 2004: Message edited by: Budi Kurniawan ]
[ September 15, 2004: Message edited by: Budi Kurniawan ]
17 years ago