File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes URL-Pattern Issue #2 - How to map ROOT URL to a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL-Pattern Issue #2 - How to map ROOT URL to a servlet?" Watch "URL-Pattern Issue #2 - How to map ROOT URL to a servlet?" New topic

URL-Pattern Issue #2 - How to map ROOT URL to a servlet?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 140

I am trying to map the ROOT URL to a servlet within the document descriptor.

I have tried;

Although this throws all files through to my servlet such as images and as such causes all kinds of errors.

What I am aiming to do is to handle all visitors to the home page using a servlet. I need to have the homepage as the ROOT URL though and not /index.jsp or /index.html etc.

Has anyone come across this issue before?

Whilst looking for an answer on Google there didn't seem to be a useful and easy answer to such a simple question.


I also need to map URLs of the form "" or "" to a servlet.

Where "text-text" could be anything, but the two words would always be separated by a hyphen.

Something like this would be possible with mod_rewrite although I cannot find any information about how to set this up whilst using Tomcat.

I guess if I could get mod_rewrite set up for Tomcat then I could rewrite the homepage to /homepage which could then be mapped to homepage.class easily via the DD.

Has anyone had any experience of this? All I ever find whilst searching Google is questions by confused people (like me :-) )


Jayr Motta
Ranch Hand

Joined: Jul 30, 2010
Posts: 110

Hi Michael,

The url-pattern tag is made to map a given url or string that match with a expression (in case of using wildcards), and about not let explicit pages like index.html or index.jsp they're mapped into the welcome-file at the deployment descriptor and you can map what you want to be the paths to look for when you receive a default path.

But i got what you're trying to do, i've made some tests and obviously you made some searchs about it and conclude that something else must be done to attend your needs. In servler/jsp mappings are three ways to map a resource: specific path (e.g.: /, directory (e.g.: /omg/*) and extension (e.g.: *.foo), and the order i've described is the order of J2EE container parse them, but if you specify a pattern that doesn't match with any of this he will match with the most close to this, and in the case of "/" is directory mapping which gets everything with starts with "/", in others words, everything.

To do what you want to do you must use a filter and inside this filter you say what you want to capture (and redirect, in your case of redirecting to the home page) and those which you don't want to do anything, there is a lot of frameworks that do that and its called "Friendly URL's frameworks", they're written to make your URL's more friendly to your user and give the same semanthic by extracting the data in a fashion way.

Try this one to a J2EE architecture:

Certainly others like this exists, but this one have a declarative way to map your urls, like an interface between your DD and your resources. Struts have something like that too in his main XML incorporated in his action mapping.

I hope this help you, if it don't or if you have any other doubt, feel free to ask me and i'll be pleased to help you!

Feel free to ask me anything!!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 140
Hi Jayr Motta, thanks for the exhaustive answer that was really helpful in understanding more about how the J2EE container works.

I will have a look into the URL rewrite filter you posted and aim to complete this at some point soon.

If I have any other questions I will be in touch.

Thanks again for the background information.

I agree. Here's the link:
subject: URL-Pattern Issue #2 - How to map ROOT URL to a servlet?
It's not a secret anymore!