分享

resin4 url_rewrite實踐

 用勿龍潛 2011-12-23
resin4 url_rewrite實踐
網上有一些resin的url_rewrite的文章,基本上都是resin3的,極少數是resin4的,但寫法也是resin3的,而且基本都是抄的同一篇文章,
本文是根據http://www.官方參考文檔實踐之後所寫。
 
1、在WEB-INF目錄下創建一個resin的web配置文件resin-web.xml,寫入以下內容:
<web-app xmlns="http:///ns/resin" xmlns:resin="urn:java:com.caucho.resin">
        <resin:Forward regexp="^/index.ihtml$" target="index.jsp"/>
        <resin:Forward regexp="^/abc-index.ihtml$" target="/abc/index.jsp"/>
        <resin:Forward regexp="^/view_([^/]+)\.ihtml$" target="/view.jsp?serverName=$1"/>
        <resin:Forward regexp="^/abc-index_([^/]*)_([^/]*)_([^/]*)\.ihtml$" target="/abc/index.jsp?akey=$1&bkey=$2&ckey=$3"/>
</web-app>
解釋一下,第一行的xmlns和xmlns:resin很重要,前者是引入resin的默認命名空間,而後者是引入前綴為resin的命名空間,後面所有的url_rewrite標籤全部定義在這個命名空間中。
第二行到第五行都是定義的rewrite規則:
第二行:將首頁index.jsp rewrite成index.ihtml,ihtml是我給自定義的偽靜態文件擴展名,因為我是將resin編譯成apache的模塊來使用的,因此需要定義一個特殊的擴展名讓apache來識別,過濾給resin處理;
第三行:將/abc目錄下的index.jsp文件rewrite成/abc-index.ihtml,我的rewrite原則是消滅目錄結構,這樣更便於搜索引擎收納,rewrite原則是“目錄1-目錄2-目錄3-目錄n-文件_參數值1_參數值2_參數值n.ihtml”;
第四行:將/view.jsp?serverName=xxxx這樣URL rewrite成/view_xxxx.ihtml,$1表示前面regexp中定義的第一對()中的匹配模式;
第五行:將/abc/index.jsp?akey=xxx&bkey=yyy&ckey=zzz這樣的URL rewrite成/abc-index_xxx_yyy_zzz.ihtml,$1、$2、$3的含義如上所述,注意在target中“&”符號要轉義成“&”,否則resin會報告錯誤的。

2、創建測試用的文件:
首頁文件:index.jsp
<%@ page contentType="text/html;charset=utf-8"%>
<%
String serverName = request.getServerName();
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>urlrewrte</title>
</head>
<body>
<a href=view_<%=serverName%>.ihtml>服務器名稱</a>
<a href=abc-index.ihtml>abc index</a>
<body>
</html>
注意兩個<a>置標符,第一個鏈接的目的文件是view_<%=serverName%>.ihtml,根據resin-web.xml文件中的定義,它的真實URL就是/view.jsp?serverName=<%=serverName%>,第二個鏈接abc-index.ihtml的真實URL就是/abc/index.jsp。
 
view.jsp文件:
<%
String serverName = request.getParameter("serverName");
out.println(serverName);
%>
點擊view_<%=serverName%>.ihtml鏈接訪問到的真實文件。
/abc/index.jsp文件:
<%@ page contentType="text/html;charset=utf-8"%>
<%
String akey = request.getParameter("akey");
String bkey = request.getParameter("bkey");
String ckey = request.getParameter("ckey");
out.print("akey = " + akey + " | bkey = " + bkey + " | ckey = " + ckey);
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>urlrewrte</title>
</head>
<body>
<form method=post action="abc-index.ihtml">
<input type=text name=akey id=akey value=<%=akey%>>
<input type=text name=bkey id=bkey value=<%=bkey%>>
<input type=text name=ckey id=ckey value=<%=ckey%>>
<input type=submit>
</form>
</body>
</html>
<a href=abc-index_<%=akey%>_<%=bkey%>_<%=ckey%>.ihtml>abc index 3key</a>
點擊/abc-index.ihtml鏈接就會訪問到這個文件,在三個文本輸入框中隨便輸入一些內容點提交之後再看最後的<a>鏈接,是不是鏈接文件名稱跟著變化了?
 
3、配置apache
關於apache與resin的編譯整合就不說了,太簡單、太自動化了。。我們來看看apache和resin的配置:
apache配置:
<VirtualHost *:80>
    ServerAdmin shinesoft@21cn.com
    DocumentRoot /home/www/html/default/webpath
    ServerName test.abc.com
    Alias /test /home/www/html/test/webpath
    ErrorLog /home/www/html/logs/test.abc.com-error_log
    CustomLog /home/www/html/logs/test.abc.com_log common
    DirectoryIndex index.html index.ihtml index.jsp index.html.var
    <Directory "/home/www/html/default/webpath">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    <Directory "/home/www/html/test/webpath">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    <LocationMatch "^/[^/]+/[^/]+\.ihtml$">
                SetHandler caucho-request
    </LocationMatch>
</VirtualHost>
來解釋一下關鍵配置,因為我發佈了兩個web應用,一個是/,一個是/test,因為我是在/test下做的rewrite測試,因此apache也要根據/test做相應的配置,Alias是將/home/www/html/test/webpath指定別名為/test,這個別名就是resin中指定的/test應用,名稱兩邊一定要一致;
<LocationMatch>是apache2中支持正則表達式的Location,"^/[^/]+/[^/]+\.ihtml$"這段正則表達式的意思是匹配“以/開頭的,後面跟上非/的任意字符的任意長度的字符串,後面再跟一個/,然後後面再跟任意多個非/字符,再跟上.ihtml”,最終的效果就是匹配“/xxxxxx/xxxxxx.ihtml”。SetHandler caucho-request的意思是只要匹配上了就交給resin處理。這個效果就是只要是/test下的所有.ihtml擴展名的文件全部交給resin處理了,這樣resin就可以接收到.ihtml文件,並根據resin-web.xml中定義的rewrite規則進行處理了。
千萬記得rewrite的工作不能讓apache來做,因為如果apache rewrite後的.ihtml文件交給resin,resin又沒有定義rewrite規則,因此會報告404文件找不到。
 
4、配置resin:resin.xml
 <host id="" root-directory="/home/www/html">
      <web-app id="/" root-directory="default/webpath"/>
      <web-app id="/test" root-directory="test/webpath"/>
 </host>
host id我沒設置,留空表示默認主機,我就一個域名的應用,因此就用這個默認的了,下面發佈了兩個web應用/和/test,/test要和apache中配置的Alias /test名稱一致,路徑指向當然也得一致,host id如果想寫上的話,也要和apache中設置的一致:test.abc.com。
 
結束。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多