页面伪静态化 java_UrlRewrite 伪静态化页面
官網(wǎng)
http://tuckey.org/urlrewrite/#download
概念
UrlRewrite 地址重寫(xiě),可以將非靜態(tài)頁(yè)面地址進(jìn)行轉(zhuǎn)換,偽裝成html后綴的網(wǎng)頁(yè)。
優(yōu)點(diǎn)
提高安全性,可以有效的避免一些參數(shù)名、ID等完全暴露在用戶(hù)面前,如果用戶(hù)隨便亂輸?shù)脑?huà),不符合規(guī)則的話(huà)直接會(huì)返回個(gè)404或錯(cuò)誤頁(yè)面,這比直接返回500或一大堆服務(wù)器錯(cuò)誤信息要好的多;
美化URL,去除了那些比如*.do之類(lèi)的后綴名、長(zhǎng)長(zhǎng)的參數(shù)串等,可以自己組織精簡(jiǎn)更能反映訪(fǎng)問(wèn)模塊內(nèi)容的URL;
更有利于搜索引擎的收入,通過(guò)對(duì)URL的一些優(yōu)化,可以使搜索引擎更好的識(shí)別與收錄網(wǎng)站的信息;
轉(zhuǎn)換規(guī)則
rule說(shuō)明
通過(guò)正則表達(dá)式對(duì)請(qǐng)求進(jìn)行匹配
實(shí)際請(qǐng)求地址
^/m/category_([0-9]+)/content_([0-9]+).html$
/main.jsp?categoryid=$1&contentid=$2
outbound-rule說(shuō)明
頁(yè)面顯示的鏈接規(guī)則表達(dá)式
轉(zhuǎn)換為實(shí)際顯示的地址
^/main.jsp\?categoryid=([0-9]+)&contentid=([0-9]+)$
/m/category_$1/content_$2.html
rule與outbound-rule需要匹配。
注意轉(zhuǎn)義字符:
& 轉(zhuǎn)義為 &
? 轉(zhuǎn)義為 /?
示例代碼
1.下載項(xiàng)目jar包并加載到項(xiàng)目工程中。
2.將urlrewrite.xml文件加載到WEB-INF目錄下,并編寫(xiě)轉(zhuǎn)換規(guī)則,編寫(xiě)規(guī)則使用正則表達(dá)式。
示例代碼
/p>
"http://tuckey.org/res/dtds/urlrewrite3.1.dtd">
^/index.html$
/index.jsp
^/login.html$
/login2.jsp
^/query.html$
/queryResult.jsp
^/m/category_([0-9]+).html$
/main.jsp?categoryid=$1
^/m/category_([0-9]+)/content_([0-9]+).html$
/main.jsp?categoryid=$1&contentid=$2
^/v/category_([0-9]+)/content_([0-9]+).html$
/view.jsp?categoryid=$1&contentid=$2
^/f/category_([0-9]+)/pageNow_([0-9]+).html$
/frame.jsp?categoryid=$1&pageNow=$2
^/p/category_([0-9]+)/pageNow_([0-9]+).html$
/proframe.jsp?categoryid=$1&pageNow=$2
^/c/category_([0-9]+)/pageNow_([0-9]+).html$
/credentails.jsp?categoryid=$1&pageNow=$2
^/p/depart_([0-9]+).html$
/people.jsp?departid=$1
^/index.jsp
/index.html
^/login2.jsp
/login.html
^/queryResult.jsp
/query.html
^/main.jsp\?categoryid=([0-9]+)$
/m/category_$1.html
^/main.jsp\?categoryid=([0-9]+)&contentid=([0-9]+)$
/m/category_$1/content_$2.html
^/view.jsp\?categoryid=([0-9]+)&contentid=([0-9]+)$
/v/category_$1/content_$2.html
^/frame.jsp\?categoryid=([0-9]+)&pageNow=([0-9]+)$
/f/category_$1/pageNow_$2.html
^/proframe.jsp\?categoryid=([0-9]+)&pageNow=([0-9]+)$
/p/category_$1/pageNow_$2.html
^/credentails.jsp\?categoryid=([0-9]+)&pageNow=([0-9]+)$
/c/category_$1/pageNow_$2.html
^/people.jsp\?departid=([0-9]+)$
/p/depart_$1.html
3.在web.xml文件中進(jìn)行聲名。
UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
UrlRewriteFilter
/*
REQUEST
FORWARD
4.Jsp頁(yè)面鏈接聲名方式
">測(cè)試
說(shuō)明:按照API的指引,java里面編寫(xiě)的鏈接應(yīng)該使用轉(zhuǎn)義字符 & 代替 & 才對(duì),但是我使用了&反而不起作用,使用&才行,所以需要大家多測(cè)試一下,而且我在本機(jī)上部署訪(fǎng)問(wèn),顯示的鏈接規(guī)則是不生效的(即:無(wú)法顯示 /m/category_1/content_2.html ),但是可以通過(guò)這樣的規(guī)則去正常訪(fǎng)問(wèn),反而部署在服務(wù)器上就正常顯示。
測(cè)試結(jié)果
訪(fǎng)問(wèn)http://xxx.com/index.html 實(shí)際訪(fǎng)問(wèn)到的是index.jsp頁(yè)面
參考案例
http://pihai.iteye.com/blog/384386
http://blog.sina.com.cn/s/blog_7a2356a60100x4fy.html
http://blog.sina.com.cn/s/blog_a72be0600101dwxl.html
總結(jié)
以上是生活随笔為你收集整理的页面伪静态化 java_UrlRewrite 伪静态化页面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: typescript 接口 java_[
- 下一篇: 电脑没u盘怎么还原系统 电脑如何无U盘还