本文的實驗站點: http://www.5a520.cn  小說520網 http://www.bt285.cn BT下載
    UrlRewriteFilter的介紹:
    UrlRewriteFilter是一個用于改寫URL的Web過濾器,類似于Apache的mod_rewrite。適用于任何Web應用服務器(如Resin,Orion,Tomcat等)。其典型應用就把動態URL靜態化,便于搜索引擎爬蟲抓取你的動態網頁。
   為什么要使動態的URL變成偽靜態的URL:
   1:為了對搜索的友好,因為有些搜索不能抓取動態頁面或是對動態抓取的頁面沒有靜態頁面高.
   2:屏蔽內部的url結構.
   3:美化url.
   UrlRewriteFilter使用:
     1.下載http://tuckey.org/urlrewrite/#download目前穩定的版本是2.6,最新版3.1,推薦使用2.6版.解壓縮后將文件考到相應的web-inf/lib和web-inf下.
     2、配置web.xml
     

<filter> 
       <filter-name>UrlRewriteFilter</filter-name> 
       <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
</filter> 
<filter-mapping> 
       <filter-name>UrlRewriteFilter</filter-name> 
       <url-pattern>/*</url-pattern> 
</filter-mapping> 


     根據自己的需要,將相應目錄下的url轉給UrlRewriteFilter來處理。
   3、配置urlwrite規則文件WEB-INF/urlrewrite.xml
  http://www.5a520.cn/book/116 會直接forward 到 http://www.5a520.cn/book.php?id-116 結果都是"創世傳奇之魔族風云 _玄幻小說_小說520網"這個標題.
  http://www.5a520.cn/bookxuanhuan/3  會直接forward 到 http://www.5a520.cn/cata.php?id=bookxuanhuan&index=3 結果都是"玄幻小說_小說520網"這個標題.
配置如下: 
 

<rule>
        <from>/book/(
[0-9]+)$</from>
        <to>/book.php?id
=$1</to>
    </rule>

 <rule>
        <from>/book(
[a-z]+)$</from>
        <to>/cata.php?id
=book$1</to>
 </rule>

 <rule>
        <from>/book(
[a-z]+)/([0-9]+)$</from>
        <to>/cata.php?id
=book$1&amp;index=$2</to>
 </rule>

   注意:
   1.urlrewrite.xml是utf-8.所以如果你要在rule上加note標簽為中文的話,也一定是要utf-8
   2.UrlRewriteFilter 最好是配置在web.xml的前面filter上,不然有可能對有些url轉變失去作用.
   3.在寫rule的時,如果有多個參數時,中間的連接符號&應該是&amp;

  下面對 urlrewrite.xml標簽的一些說明:
  urlrewrite屬性:有僅只有一個.
  rule屬性::至少一個. 

<name> 屬性(可選) 
    <rule>
    <name>World Rule</name>
    <from>^/world/(
[a-z]+)/([a-z]+)$</from>
    <to>/world.jsp?country
=$1&amp;city=$2</to>
    </rule>
  <note>屬性(可選)
    <rule>
    <name>World Rule</name>
    <note>
        Cleanly redirect world requests to JSP
,
        a country and city must be specified.
        </note>
    <from>^/world/(
[a-z]+)/([a-z]+)$</from>
    <to>/world.jsp?country
=$1&amp;city=$2</to>
    </rule>

    <condition>屬性(可選)
    可以對時間,方法,來源,端口,類型等進行設置,如
    <condition name="user-agent" operator="notequal">Mozilla/[1-4]</condition> 客戶端游覽器不是Mozilla14版本以下可以訪問.
    

<condition type="user-in-role" operator="notequal">bigboss</condition> 是bigboss不能訪問.
    <condition name
="host" operator="notequal">www.example.com</condition> 主機是www.example.com不能訪問
    <condition type
="method" next="or">PROPFIND</condition> 下個rule是PROPFIND可以訪問
    <condition type
="method">PUT</condition> 是put類型

    type屬性:
    最主要就是 forward (default):在客戶端URL是不轉向的 redirect 在客戶端URL是轉向的,所以一般采用 forward
 set屬性:這個有點像apache中的rewrite強大之處了.除了下面的設置client,還可以設置cookie,content-   type,charset,header,request 
   

<rule>
    <condition name
="user-agent">Mozilla/3\.0 (compatible; AvantGo .*)</from>
    <from>.*</from>
    <set name
="client">AvantGo</set>
    </rule>
    <rule>
    <condition name
="user-agent">UP\.Browser/3.*SC03 .* </from>
    <from>.*</from>
    <set name
="client">Samsung SCH-6100</set>
    </rule>