views:

719

answers:

3

Back in my ASP.NET days, I used URLRewriter.NET to do dynamic URL Rewrites. Basically, it's an HTTPModule that intercepts page requests and rewrites the URLs according to rules that you define, very similar to MOD_REWRITE. However, it also lets you define a "Custom Transform," a class with a single method that does URL translations for you on-the-fly. You can have this method hit the DB, access the Application[] collection, pretty much do anything your heart desires.

Is there any equivalent to this in the J2EE world? I want to be able to rewrite URLs dynamically and delegate this rewriting to some Java code. I do NOT want to just set up a list of static rewrites. Likewise, it needs to do actual URL masking, and NOT 3XX redirects.

If there isn't anything out there that does this, how would I go about building this functionality myself?

+2  A: 

Have you investigated Servlet Filters? I have not attempted to modify the URL directly and I believe the parameters would be pre-parsed into the request object, but we use the filters extensively for parsing URLs and putting path info into the DB for other Servlet and JSP use.

You could very easily wrap the request object as it chains through to the target Servlets.

Xepoch
+1: filters is the way to go. Use `HttpServletRequest#getRequestURI()` to parse the pathinfo.
BalusC
+2  A: 

How about this: http://tuckey.org/urlrewrite/

Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server (such as Resin, Orion or Tomcat), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.

skaffman
It's a powerful tool and we use it on a lot of projects, but it relies on a hard coded list of rules in an XML document. It's definitely the way to go if you don't need to make it up on the fly.
Chris Hall
A: 

The other option is write your won Servlet . Now write some URL manipulation code using the request object and URL found and call the intended page action or Servlet again.

Eg.

you should call Servlet using URL

domain/urlServlet/param1/param2/...

And your Servlet will translate it to

domain/requestedPage?p1=param1&p2=param2...

Umesh Aawte