生活随笔
收集整理的這篇文章主要介紹了
JSP URL重写-urlrewrite
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://blog.csdn.net/mr_tank_/article/details/11892965
URL重寫的目的不言而喻,首先引入urlrewrite-4.0.0.jar【或者其他版本】包,可以從官方下載。
1、web.xml【官方配置】
[html] view plaincopyprint?
<? 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"> ?? ????< display-name > </ display-name > ?? ????< welcome-file-list > ?? ????????< welcome-file > index.jsp</ welcome-file > ?? ????</ welcome-file-list > ?? ?? ?????? ????< 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-app > ??
<?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"><display-name></display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- URL重寫配置 --><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><!-- 攔截所有URL --></filter-mapping>
</web-app>
2、urlrewrite.xml
[html] view plaincopyprint?
<? 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"> ?? ?? <!--?? ?? ????Configuration?file?for?UrlRewriteFilter?? ????http://tuckey.org/urlrewrite/?? ?? --> ?? < urlrewrite > ????????? ?????< rule > ????? ?????????? ????????< from > admin/([0-9]+)/(.*).shtml/(.*)</ from > ?? ????????< to > /admin_login.jsp?id =$1&name =$2&keyword =$3</ to > ???? ????</ rule > ?????
<?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"><!--Configuration file for UrlRewriteFilterhttp://tuckey.org/urlrewrite/-->
<urlrewrite><!--自定義匹配--><rule> <!-- <from>^/admin/(.*)(.*)</from> --><from>admin/([0-9]+)/(.*).shtml/(.*)</from><to>/admin_login.jsp?id=$1&name=$2&keyword=$3</to> </rule> ?
[html] view plaincopyprint?
?????? ????< rule > ?? ????????< note > ?? ????????????The?rule?means?that?requests?to?/test/status/?will?be?redirected?to?/rewrite-status?? ????????????the?url?will?be?rewritten.?? ????????</ note > ?? ????????< from > /test/status/</ from > ?? ????????< to ?type ="redirect" > %{context-path}/rewrite-status</ to > ?? ????</ rule > ?? ?? ?? ????< outbound-rule > ?? ????????< note > ?? ????????????The?outbound-rule?specifies?that?when?response.encodeURL?is?called?(if?you?are?using?JSTL?c:url)?? ????????????the?url?/rewrite-status?will?be?rewritten?to?/test/status/.?? ?? ????????????The?above?rule?and?this?outbound-rule?means?that?end?users?should?never?see?the?? ????????????url?/rewrite-status?only?/test/status/?both?in?thier?location?bar?and?in?hyperlinks?? ????????????in?your?pages.?? ????????</ note > ?? ????????< from > /rewrite-status</ from > ?? ????????< to > /test/status/</ to > ?? ????</ outbound-rule > ?? ?? ?? ????<!--?? ?? ????INSTALLATION?? ?? ????????in?your?web.xml?add...?? ?? ????????< 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 > ?? ?? ?? ?????EXAMPLES?? ?? ?????Redirect?one?url?? ????????< rule > ?? ????????????< from > /some/old/page.html</ from > ?? ????????????< to ?type ="redirect" > /very/new/page.html</ to > ?? ????????</ rule > ?? ?? ????Redirect?a?directory?? ????????< rule > ?? ????????????< from > /some/olddir/(.*)</ from > ?? ????????????< to ?type ="redirect" > /very/newdir/$1</ to > ?? ????????</ rule > ?? ?? ????Clean?a?url?? ????????< rule > ?? ????????????< from > /products/([0-9]+)</ from > ?? ????????????< to > /products/index.jsp?product_id =$1</ to > ?? ????????</ rule > ?? ????eg,?/products/1234?will?be?passed?on?to?/products/index.jsp?product_id =1234 ?without?the?user?noticing.?? ?? ????Browser?detection//瀏覽器檢測?? ????????< rule > ?? ????????????< condition ?name ="user-agent" > Mozilla/[1-4]</ condition > ?? ????????????< from > /some/page.html</ from > ?? ????????????< to > /some/page-for-old-browsers.html</ to > ?? ????????</ rule > ?? ????eg,?will?pass?the?request?for?/some/page.html?on?to?/some/page-for-old-browsers.html?only?for?older?? ????browsers?whose?user?agent?srtings?match?Mozilla/1,?Mozilla/2,?Mozilla/3?or?Mozilla/4.?? ?? ????Centralised?browser?detection?? ????????< rule > ?? ????????????< condition ?name ="user-agent" > Mozilla/[1-4]</ condition > ?? ????????????< set ?type ="request" ?name ="browser" > moz</ set > ?? ????????</ rule > ?? ????eg,?all?requests?will?be?checked?against?the?condition?and?if?matched?? ????request.setAttribute("browser",?"moz")?will?be?called.?? ?? ????--> ?? ?? </ urlrewrite > ??
<!-- 官方示例--><rule><note>The rule means that requests to /test/status/ will be redirected to /rewrite-statusthe url will be rewritten.</note><from>/test/status/</from><to type="redirect">%{context-path}/rewrite-status</to></rule><outbound-rule><note>The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)the url /rewrite-status will be rewritten to /test/status/.The above rule and this outbound-rule means that end users should never see theurl /rewrite-status only /test/status/ both in thier location bar and in hyperlinksin your pages.</note><from>/rewrite-status</from><to>/test/status/</to></outbound-rule><!--INSTALLATIONin your web.xml add...<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>EXAMPLESRedirect one url<rule><from>/some/old/page.html</from><to type="redirect">/very/new/page.html</to></rule>Redirect a directory<rule><from>/some/olddir/(.*)</from><to type="redirect">/very/newdir/$1</to></rule>Clean a url<rule><from>/products/([0-9]+)</from><to>/products/index.jsp?product_id=$1</to></rule>eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.Browser detection//瀏覽器檢測<rule><condition name="user-agent">Mozilla/[1-4]</condition><from>/some/page.html</from><to>/some/page-for-old-browsers.html</to></rule>eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for olderbrowsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.Centralised browser detection<rule><condition name="user-agent">Mozilla/[1-4]</condition><set type="request" name="browser">moz</set></rule>eg, all requests will be checked against the condition and if matchedrequest.setAttribute("browser", "moz") will be called.--></urlrewrite>
3、項目結(jié)構(gòu):
4、admin_login.jsp頁面代碼:
[html] view plaincopyprint?
< body > ??????Admin?Login?Page.?? ????< br > ?? ????< %=request.getParameter("id")%> < br > ?? ????< %=request.getParameter("name")%> < br > ?? ????< %=request.getParameter("keyword")%> ?? </ body > ??
<body>Admin Login Page.<br><%=request.getParameter("id")%><br><%=request.getParameter("name")%><br><%=request.getParameter("keyword")%>
</body>
測試結(jié)果:
http://123.125.115.53/view/1002788.html?fromTaglist
URL重寫就是首先獲得一個進入的URL請求然后把它重新寫成網(wǎng)站可以處理的另一個URL的過程。舉個例子來說,如果通過瀏覽器進來的URL是“UserProfile.aspx?ID=1”那么它可以被重寫成 “UserProfile/1.aspx”,這樣的URL,這樣的網(wǎng)址可以更好的被網(wǎng)站所閱讀。
如果瀏覽器不支持Cookie或用戶阻止了所有Cookie,可以把會話ID附加在HTML頁面中所有的URL上,這些頁面作為響應(yīng)發(fā)送給客戶。這樣,當(dāng)用戶單擊URL時,會話ID被自動作為請求行的一部分而不是作為頭行發(fā)送回服務(wù)器。這種方法稱為URL重寫(URL rewriting)。
一般來說,URL重寫是支持會話的非常健壯的方法。在不能確定瀏覽器是否支持Cookie的情況下應(yīng)該使用這種方法。然而,使用URL重寫應(yīng)該注意下面幾點:
1.如果使用URL重寫,應(yīng)該在應(yīng)用程序的所有頁面中,對所有的URL編碼,包括所有的超鏈接和表單的action屬性值。
2.應(yīng)用程序的所有的頁面都應(yīng)該是動態(tài)的。因為不同的用戶具有不同的會話ID,因此在靜態(tài)HTML頁面中無法在URL上附加會話ID。
3.所有靜態(tài)的HTML頁面必須通過Servlet運行,在它將頁面發(fā)送給客戶時會重寫URL。
?
總結(jié)
以上是生活随笔 為你收集整理的JSP URL重写-urlrewrite 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。