Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do dynamic URL Rewriting in J2EE

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?

like image 782
sangfroid Avatar asked Nov 09 '09 22:11

sangfroid


People also ask

Which method is used in URL rewriting in j2ee?

We can encode URL with HttpServletResponse encodeURL() method and if we have to redirect the request to another resource and we want to provide session information, we can use encodeRedirectURL() method.

What is URL rewriting used for?

URL rewriting allows URLs to be more easily remembered by the user. When the URL is entered into the Web server, the URL rewrite engine modifies the syntax behind the scenes to enable the appropriate Web page or database item to be retrieved.

How do you rewrite a URL in HTML?

To rewrite urls you need to configure the webserver to do that (for example in apache with . htaccess . But if you want to do that without use server configuration, a bad solution exists: make a folder with the name of the url and put the html into that with the name index. php .


2 Answers

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.

like image 148
Jé Queue Avatar answered Sep 28 '22 06:09

Jé Queue


How about these:

http://ocpsoft.com/prettyfaces/

PrettyFaces is an OpenSource Filter-based Servlets extension with enhanced support for JavaServer Faces – JSF 1.1, 1.2 and 2.0 – enabling creation of bookmark-able, pretty URLs. PrettyFaces solves the “RESTful URL” problem elegantly, including features such as: page-load actions, seamless integration with faces navigation, dynamic view-id assignment, managed parameter parsing, and configuration-free compatibility with other web frameworks.

http://ocpsoft.com/rewrite/

Rewrite is an OpenSource Filter-based Servlets extension for Java – enabling creation of bookmark-able, pretty URLs. Rewrite solves the “RESTful URL” problem elegantly, including features such as: page-load actions, managed parameter parsing, seamless integration with CDI, Spring, and configuration-free compatibility with other web frameworks.

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.

like image 21
skaffman Avatar answered Sep 28 '22 06:09

skaffman