Question

I want to write my own version of url rewriting for my app, but I don't know how to change the url of the incoming request in the filter.

I tried just forwarding to the rewritten url, but that makes it so all other filters in the chain are not called.

Was it helpful?

Solution

The right way to do it is to create a subclass of HttpServletRequestWrapper, override its getRequestURI() and other methods to return the new URL, and wrap the request with it. So you don't have to change the other filter mappings.

OTHER TIPS

but that makes it so all other filters in the chain are not called.

Just reconfigure the other filters to listen on the new url-pattern.

Why write your own when you have many well-written options done for you already? You can use Tuckey URL-rewrite filter, but I would also have a look OCPsoft PrettyFaces or OCPsoft Rewrite for this:

With PrettyFaces:

create WEB-INF/pretty-config.xml

<url-mapping>
   <pattern value="/#{username}" />
   <view-id value="/profile.jsp" />
</url-mapping>

This will automatically put the value of the URL "username" into the request parameter named "username", and will hide the original URL and replace it with the new one in links in your HTML pages.

With Rewrite:

Here is the same thing using Rewrite, which is a bit more explicit, but also more powerful and configurable.

ConfigurationBuilder.begin()
   .addRule(Join.path("/{username}").to("/profile.jsp")
                .where("username").bindsTo(Request.parameter("username")));

I hope this helps.

~Lincoln

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top