Friendly URLs in J2EE September 4, 2007Posted by Phill in General J2EE, Struts.
Tags: friendly urls, j2ee, mod_rewrite
If you’re dealing with applications which are eventually going to be exposed to the public, there’s a good chance that there will be some constraints on URLs (particularly with corporate websites and so on). For example, if you’re writing an application which allows people to view their billing details online, a requirement might be for the URL to be http://www.company.com/billing/BillingDetails, as opposed to http://www.company.com/billing/GetBillingDetails.do.
There are a couple of advantages to this approach:
- The details of the implementation are hidden, i.e. the same URL could be implemented by potentially any framework – Struts, JSP, Spring, whatever. Even if you change the underlying technology (to, for example, .NET), the URLs can probably remain the same.
- URLs are more ‘friendly’, meaning that people will be more likely to remember them!
- If the URLs have to be changed, you don’t have to change any of the application logic code (i.e. struts-config.xml) – you can just change the mapping configuration.
How can you accomplish something like this? In Apache you can use mod_rewrite. I know that some application servers do use Apache as the underlying web server (for example, Oracle HTTP Server, part of Oracle Application Server, uses Apache), and it would probably be possible to use mod_rewrite there.
However, I wouldn’t recommend this approach – it’s specific to each application server. A far more ‘J2EE’ solution would be to use a technology common to all J2EE servers – servlet filters. And fortunately, someone else has already done the work: you can use this in a similar way to mod_rewrite. The URL the user sees (/BillingDetails) gets redirected internally to your Struts Action (/GetBillingDetails.do), or whatever else you have internally.