Thursday, 3 July 2008

Tomcat Root Context and Blank Page

Prelude:
Arggh have been bad and not updating the blog (while I have been busy with UK visa application...more abt that later...as well as taking on a new project at work recently). So there have been a lot of learning done over the past month or so! Anyway let me document one of my great (and painful) discovery that I have made during work over the past week or so!!! : O


Tomcat and its Root Context:
For the past few weeks or so I have been moved into a new project at work where I get to work with new technologies and some really talented senior people!!! It's exciting yet scary (coz I know I'll be doing crazy stuffs)!!!

Yip that's right, in fact, over the past weeks I have already got myself involved in the craziness as I had to poke around the SOURCE CODE of Jakarta Tomcat to see how it sets up its exception page!

Anyway after days of investigation I finally found out the why a root context is important!!!
  • With a Root Context:
    A Root Context is the context that Tomcat serves when you type in http://localhost:8080/ (assuming tomcat is setup to use port 8080), i.e. context name is an empty String "".

    If you have a root context, not only will tomcat return the 'welcome page' when http://localhost:8080/ is requested. Also, if you request for a non-existing context e.g. http://localhost:8080/foobar, it will return you with a tomcat 404 exception page.

  • Without a Root Context:
    However, if the tomcat server DOES NOT have a root context, the result is that not only will you get a blank page returned if you type in http://localhost:8080/. You will also get a blank page if you request for a non-existing context.
Now I finally realise the importance of having a root context. Not only does it affect the pages that get returned to the user but also if you want server-wide behaviour, you will need to edit the deployment descriptor (web.xml) inside the root context. For example, if you want all 404 exception to be redirected to an error page, you will need to define the tag inside the root context's web.xml. : )

3 comments:

  1. Hi, I am facing this root context error.
    My tomcat server is always returning a blank page. Can you put more light on this topic. How do i get my root context right.

    Thanks n advance

    ReplyDelete
  2. Hi Saurabh,

    Sorry for replying so late as I have been quite busy over the past weeks since holiday.

    Are you actually running tomcat as an application or are you running it as embedded in your web application?

    Because I was actually dealing with an embedded tomcat, so I had to explicitly define a root context. However, if you actually got your tomcat from an installer, I believe it should be pretty trivial and the Tomcat welcome page should be at your root context by default.

    ReplyDelete