wood burning stoves*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

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

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Hello,

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

I have tried;
<url-pattern>/</url-pattern>

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.

Additional:

I also need to map URLs of the form "www.example.com/text-text/" or "www.example.com/text-text" 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 :-) )

Thanks
Michael

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.: /foo.bar), 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: http://code.google.com/p/urlrewritefilter/

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!
www.BlackBeltFactory.com/ui#!/ref=jmotta, SCJP 6, OCWCD JEE5, OCE EJB JEE6
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
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.

Michael
 
jQuery in Action, 2nd edition
 
subject: URL-Pattern Issue #2 - How to map ROOT URL to a servlet?
 
Similar Threads
[Tomcat6/mod_jk/Apache2.2] JkMount and Url patterns
Tomcat on Linux tutorial
URL-Pattern Issue
URLRewriteFilter for ROOT?
question about servlet-mapping url-patterns