Web Server: Custom URL Rewrite Rule Tutorial
From Resin 4.0 Wiki
(Difference between revisions)
Line 1: | Line 1: | ||
{{Config}} {{WebServer}} {{Cookbook}} | {{Config}} {{WebServer}} {{Cookbook}} | ||
+ | |||
+ | If you want to take control of URL processing, you can create a custom Resin rewrite rule. | ||
+ | Just like Resin's <resin:Forward> tag, your custom rule can match on a URL, and even have | ||
+ | rewrite predicates like Resin's <resin:IfSecure> or <resin:IfHeader> or <resin:IfFileExists>. | ||
+ | |||
+ | A rewrite rule has two components: | ||
+ | |||
+ | * the action, which is a servlet javax.servlet.FilterChain | ||
+ | * the rule itself, which implements com.caucho.rewrite.DispatchRule | ||
+ | |||
+ | Once you write the action and the rule, you can use it in your WEB-INF/resin-web.xml just like any other rewrite rule. | ||
+ | |||
+ | In this example, we'll create a trivial rewrite rule that just displays the request URI and the target URI. | ||
+ | |||
+ | == Using the rewrite rule in the resin-web.xml == | ||
+ | |||
+ | To use your rule, you'll create an instance in the <web-app> using the standard CDI XML syntax. | ||
=== WEB-INF/resin-web.xml === | === WEB-INF/resin-web.xml === |
Revision as of 00:00, 25 January 2012
If you want to take control of URL processing, you can create a custom Resin rewrite rule. Just like Resin's <resin:Forward> tag, your custom rule can match on a URL, and even have rewrite predicates like Resin's <resin:IfSecure> or <resin:IfHeader> or <resin:IfFileExists>.
A rewrite rule has two components:
- the action, which is a servlet javax.servlet.FilterChain
- the rule itself, which implements com.caucho.rewrite.DispatchRule
Once you write the action and the rule, you can use it in your WEB-INF/resin-web.xml just like any other rewrite rule.
In this example, we'll create a trivial rewrite rule that just displays the request URI and the target URI.
Contents |
Using the rewrite rule in the resin-web.xml
To use your rule, you'll create an instance in the <web-app> using the standard CDI XML syntax.
WEB-INF/resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:org.example.mypkg"> <mypkg:MyDebugRewrite regexp="^/test/" target="/new/"/> </web-app>
qa/MyDebugFilterChain.java
package qa; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class TestFilterChain implements FilterChain { private String _target; public TestFilterChain(String target) { _target = target; } public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) request; PrintWriter out = response.getWriter(); out.println("URI: " + req.getRequestURI()); out.println("target: " + _target); } }
MyDebugRewrite.java
package qa; import javax.servlet.*; import javax.servlet.http.*; import com.caucho.rewrite.*; public class Test extends AbstractTargetDispatchRule { @Override public FilterChain createDispatch(DispatcherType type, String uri, String queryString, String target, FilterChain next) { return new TestFilterChain(target); } }