UrlRewrite:
UrlRewrite就是我們通常說的地址重寫,用戶得到的全部都是經過處理後的URL地址,類似於Apache的mod_rewrite。將我們的動態網頁地址轉化爲靜態的地址,如html、shtml,還可以隱藏網頁的真正路徑,
比如:有時候需要將xxx.com/news/ type1/001.jsp 轉化成顯示路徑爲xxx.com/news_type1_001.html
有點如下:
一:提高安全性,屏蔽內部的url結構.
二:美化URL
三:更有利於搜索引擎的收入,通過對URL的一些優化,可以使搜索引擎更好的識別與收錄網站的信息.
下載地址:
官網下載: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#filterparams
實例展示
實例應用版本urlrewritefilter-4.0.3. Tomcat服務器端口定製爲80
1. 創建web項目,增加 urlrewritefilter-4.0.3.jar 到 WEB-INF/lib
2. 在WEB-INF/web.xml 增加urlrewritefilter過濾器 (near the top above any servlet mappings)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 加到任何servlet映射的頂部,不然可能有些路徑不能被過濾到
http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html
-->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<!--
設備文件重加載間隔 (0默示隨時加載, -1默示不重加載, 默認-1)
-->
<init-param>
<param-name>confReloadCheckInterval</param-name>
<param-value>60</param-value>
</init-param>
<!-- 自定義配置文件的路徑,是相對context的路徑,(默認位置 /WEB-INF/urlrewrite.xml) -->
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
<!--
設置日誌級別(將被記錄到日誌中)
可以爲: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, slf4j,
比如 sysout:DEBUG(設置到控制檯調試輸出級別)
(默認級別 WARN) -->
<init-param>
<param-name>logLevel</param-name>
<param-value>DEBUG</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
如果覺得/*這樣的通配,並不符合我的預期,我只想對部分路徑進行URL的重寫,/*可能會造成我想象不到的或者是許微不足道的性能浪費.我把它改成了我需要的:
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/member/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/article/*</url-pattern>
</filter-mapping>
更多請參考: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html
3. 因爲上面我們通過confPath定義了配置文件的路徑,其實該默認位置就是在/WEB-INF/urlrewrite.xml,爲了更能說明問題,所以顯示指定下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite
PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
<from>/page/(.*).html</from>
<to>/index.jsp?page=$1</to>
</rule>
<rule>
<from>^/user/([a-z]+)/([0-9]+)$</from>
<to>/index.jsp?nickname=$1&age=$2</to>
</rule>
</urlrewrite>
此時我們就可以通過url進行模擬了.
注意:
1.urlrewrite.xml是utf-8.所以如果你要在rule上加note標籤爲中文的話,也一定是要utf-8.
2.UrlRewriteFilter 最好是配置在web.xml的前面filter上,不然有可能對有些url轉變失去作用.
3.urlrewrite屬性:有僅只有一個,rule屬性::至少一個.
4.在寫rule的時,如果有多個參數時,中間的連接符號&應該是&
5.rule是url重寫規則,from是顯示出來的地址,to是映射的實際地址,$1是重寫參數,它的值與from中的正則表達式是一一對應,可以爲多個,()裏是匹配的正則表達式, 在正則表達式^指定字符的串開始,$爲指定結束
6.對於中文參數要使用(.*)作爲參數轉義.
4.重寫url演示
實例1
<rule>
<from>/page/(.*).html</from>
<to>/index.jsp?currentPage=$1</to>
</rule>
index.jsp中的內容
<body>
<%
String current = request.getParameter("currentPage");
%>
當前頁碼<%=current %>
</body>
執行效果如下:
實例2
Rule規則
<rule>
<name>World Rule</name>
<from>^/user/([a-z]+)/([0-9]+)$</from>
<to>/index.jsp?nickname=$1&age=$2</to>
</rule>
index.jsp中的內容
<body>
<%
String username = request.getParameter("nickname");
int age = Integer.parseInt(request.getParameter("age"));
%>
用戶名: <%=username %> 年齡: <%=age %> <br>
</body>
執行效果如下:
所以,當我們在url中輸入”http://localhost/urlrewrite/user/dennisit/23”時,實際執行的就是”http://localhost/urlrewrite/index.jsp?nickname=dennisit&age=23”
實例3
同理rule規則如下時
<rule>
<from>^/page/(.*)$</from>
<to type="redirect">/page/$1.action</to>
</rule>
這樣我訪問的:http://localhost/urlrewrite/page/test
則跳轉到: http://localhost/urlrewrite/page/test.action
實例4
Rule規則
<rule>
<from>^/([a-z]+)/([a-z]+)/([a-z]+)$</from>
<to>/$1.do?method=$2&uuid=$3</to>
</rule>
在index.jsp中添加如下鏈接:
<a href="process/show/index">跳轉</a>
當點擊該鏈接,
地址欄中顯示url是:http://localhost/urlrewrite/process/show/index,
其實際執行路徑是:http://localhost/urlrewrite/process.do?method=show&uuid=index
<script type="text/javascript"><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首頁橫幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>