首先要在web.xml中声明一个Filter
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后在WEB-INF目录下新建urlrewrite.xml
在其中进行重写规则的定义,它使用正则表达式来进行规则的定义
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
-->
<urlrewrite>
<rule>
<from>/test.html</from>
<to type="redirect">%{context-path}/page.html</to>
</rule>
<rule>
<from>/param/(.*)</from>
<to>/param.jsp?param=$1</to>
</rule>
</urlrewrite>
上面是我的一个简单的测试
<rule>
<from>/test.html</from>
<to type="redirect">%{context-path}/page.html</to>
</rule>
是将test.html的访问请求转发给page.html
<rule>
<from>/param/(.*)</from>
<to>/param.jsp?param=$1</to>
</rule>
将param.jsp?param=111这种请求重写为/param/111
里面有详细的例子,大家可以自己看下