1、Urlrewritefilter說明及優點html
Urlrewritefilter,經過java的Filter過濾器對URL進行重寫,用戶獲得的所有都是通過處理後的URL地址,本質上經過僞地址進行頁面跳轉,隱藏真實地址,達到掩人耳目的目的,哈哈。java
有如下優點:
1:提升安全性,能夠有效的避免一些參數名、ID等徹底暴露在用戶面前,若是用戶隨便亂輸的話,不符合規則的話直接會返回個404或錯誤頁面,這比直接返回500或一大堆服務器錯誤信息要好的多
2:美化URL,去除了那些好比*.do,*.action之類的後綴名、長長的參數串等,能夠本身組織精簡更能反映訪問模塊內容的URL web
3:更有利於搜索引擎的收入,經過對URL的一些優化,可使搜索引擎更好的識別與收錄網站的信息正則表達式
2、網絡資源安全
一、下載地址 官網:http://tuckey.org/urlrewrite/ google code :https://code.google.com/p/urlrewritefilter/服務器
二、文檔使用說明:http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html網絡
3、使用步驟app
一、在web.xml中加入如下代碼 jsp
<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> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
二、在urlrewrite.xml配置文件中進行地址映射規則,使用正則表達式svn
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"> <urlrewrite> <rule> <from>^/newsInfo-list-([0-9]+).shtml$</from> <to>/NewsInfo.do?method=list&mod_id=$1</to> </rule> <outbound-rule> <from>/NewsInfo.do\?method=list&mod_id=([0-9]+)$</from> <to>/newsInfo-list-$1.shtml</to> </outbound-rule> </urlrewrite>
三、JSP中使用以下地址
<c:url var="url_1001001000" value="/NewsInfo.do?method=list&mod_id=1001001000" /> <li><a href="${url_1001001000}">測試地址</a></li>
官網文檔中提供以下使用方式:
Using the example above JSP's with the code <a href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a> will output <a href="/world/usa/nyc">nyc</a> Or JSTL <a href="<c:url value="/world.jsp?country=${country}&city=${city}" />">nyc</a> will output <a href="/world/usa/nyc">nyc</a> Note, If you are using JSTL (ie, <c:url) this will work also.
四、基本原理
jsp頁面地址--> 服務器filter過濾 --> 調用urlrewrite.xml映射規則 --> 服務器響應 --> 轉換成僞地址
五、小結:Urlrewritefilter簡單易學易用,是Java Web開發中地址隱藏的不二選擇。