利用URLrewrite實現僞靜態地址輸出

今天項目過程中發現很多超鏈接把參數全部暴露在瀏覽器地址欄,甚是擔心。幸好,被我發現了一種實現技術,哈哈。。。。

1.下載urlrewrite,官方下載地址:http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip  有可能直接下載不了,你可以找個***代理網站打開下載。

如:http://www.cnproxy.com/webproxy.php 

下載得到urlrewritefilter-3.2.0.zip 解壓後 找到lib下的urlrewrite-3.2.0.jar包拷貝到你的工程lib下,拷貝urlrewrite.xml到web_INF下。

2.在web.xml中Filter的形式加載urlrewrite註明過濾的內容:

  1. <filter> 
  2.         <filter-name>UrlRewriteFilter</filter-name> 
  3.         <filter-class> 
  4.             org.tuckey.web.filters.urlrewrite.UrlRewriteFilter  
  5.         </filter-class> 
  6.     </filter> 
  7.     <filter-mapping> 
  8.         <filter-name>UrlRewriteFilter</filter-name> 
  9.         <url-pattern>/*</url-pattern> 
  10.     </filter-mapping> 

3.修改urlrewrite.xml

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"   
  3. "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">   
  4.  
  5. <!--   
  6.  
  7. Configuration file for UrlRewriteFilter   
  8. http://tuckey.org/urlrewrite/   
  9.  
  10. -->   
  11. <urlrewrite>   
  12.     <rule>   
  13.         <from>^/([a-z]+)$</from>   
  14.         <to type"forward" >/world.jsp?id=$1</to>   
  15.     </rule>   
  16.     <rule> 
  17.         <from>^/world/(.*)$</from> 
  18.         <to>/world.jsp?tid=$1</to> 
  19.     </rule> 
  20.     <rule> 
  21.         <from>^/(.*).html$</from> 
  22.         <to>/test1/$1.jsp</to> 
  23.     </rule> 
  24.       
  25.     <outbound-rule>   
  26.         <note>   
  27.         The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)   
  28.         the url /rewrite-status will be rewritten to /test/status/.   
  29.           
  30.         The above rule and this outbound-rule means that end users should never see the   
  31.         url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks   
  32.         in your pages.   
  33.         </note>   
  34.         <from>/rewrite-status</from>   
  35.         <to>/test/status/</to>   
  36.     </outbound-rule> 
  37. </urlrewrite>  

rule是url重寫規則,from是顯示出來的地址,to是映射的實際地址,$1是重寫參數,可以爲多個,()裏是匹配的正則表達式.
好了,在項目中新建world.jsp,啓動tomcat,輸入
http://localhost:8080/Uset/world/1

Uset是你的工程名

實際上訪問的是http://localhost:8080/Uset/world.jsp?tid=1
這樣就簡單的實現了僞靜態的效果


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章