Url Rewrite Filter 使用全攻略
来源:互联网 发布:淘宝手机端的网址 编辑:程序博客网 时间:2024/06/05 02:50
Url Rewrite Filter 使用全攻略 收藏
1、什么是Url Rewrite Filter, Url Rewrite Filter可以用来做什么?
Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE compliant web application server (such as Resin, Orion or Tomcat), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.
URL rewriting is very common with Apache Web Server (see mod_rewrite's rewriting guide) but has not been possible in most java web application servers. The main things it is used for are:
URL Tidyness / URL Abstraction - keep URLs tidy irrespective of the underlying technology or framework (JSP, Servlet, Struts etc).
Browser Detection - Allows you to rewrite URLs based on request HTTP headers (such as user-agent or charset).
Date based rewriting - Allows you to forward or redirect to other URL's based on the date/time (good for planned outages).
Moved content - enable a graceful move of content or even a change in CMS.
Tiny/Friendly URL's (i.e. blah.com/latest can be redirected to blah.com/download/ver1.2.46.2/setup.exe)
A Servlet mapping engine (see Method Invocation)
UrlRewriteFilter uses an xml file, called urlrewrite.xml (it goes into the WEB-INF directory), for configuration. Most parameters
2、参考资料
http://tuckey.org/urlrewrite/ 官方站点
http://code.google.com/p/urlrewritefilter/ goole code 目前下载已经移到这上面了。
3、如何使用
3.1、安装 jar
下载 urlrewrite-3.2.0.jar 并将其复制到WEB-INF/lib目录
3.2、配置web.xml
基本上复制以下代码就可以了:
view plaincopy to clipboardprint?
<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>
</filter-mapping>
<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>
</filter-mapping>
3.3、配置rule重写规则
将用户访问的伪地址转成真实的地址。
view plaincopy to clipboardprint?
<rule>
<from>^/([a-zA-Z0-9/-]{36}).shtml$</from>
<to>/News.do?method=view&uuid=$1</to>
</rule>
<rule>
<from>^/([a-zA-Z0-9/-]{36}).shtml$</from>
<to>/News.do?method=view&uuid=$1</to>
</rule>
访问from配置节,实际上访问的是to配置节
3.4、配置outbound-rule重写规则
将页面上的真实地址,转换成伪地址,用此配置节,可以使程序和配置之间达到透明,程序唯一要做的是将要映射的地址
view plaincopy to clipboardprint?
Using the example above JSP's with the code
<a href="<%= response.encodeURL(" mce_href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>
will output
<a href="/world/usa/nyc" mce_href="world/usa/nyc">nyc</a>
Or JSTL
<a href="<c:url value=" mce_href="<c:url value="/world.jsp?country=${country}&city=${city}" ></a>">nyc</a>
will output
<a href="/world/usa/nyc" mce_href="world/usa/nyc">nyc</a>
Note, If you are using JSTL (ie, <c:url) this will work also.
Using the example above JSP's with the code
<a href="<%= response.encodeURL(" mce_href="<%= response.encodeURL("/world.jsp?country=usa&city=nyc") %>">nyc</a>
will output
<a href="/world/usa/nyc" mce_href="world/usa/nyc">nyc</a>
Or JSTL
<a href="<c:url value=" mce_href="<c:url value="/world.jsp?country=${country}&city=${city}" ></a>">nyc</a>
will output
<a href="/world/usa/nyc" mce_href="world/usa/nyc">nyc</a>
Note, If you are using JSTL (ie, <c:url) this will work also.
注意:<c:url 中不能将&换成&官方文档是错的!
view plaincopy to clipboardprint?
<outbound-rule>
<from>^/News.do/?method=view&uuid=([a-zA-Z0-9/-]{36})$</from>
<to>/$1.shtml</to>
</outbound-rule>
<outbound-rule>
<from>^/News.do/?method=view&uuid=([a-zA-Z0-9/-]{36})$</from>
<to>/$1.shtml</to>
</outbound-rule>
注意:此配置?要换成/? &要换成&
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/KimSoft/archive/2009/05/17/4194853.aspx
- Url Rewrite Filter 使用全攻略
- Url Rewrite Filter 使用全攻略
- Url Rewrite Filter 使用全攻略
- apache rewrite规则全攻略
- 使用url rewrite重写
- WebWork 2.2 Filter,Url Rewrite,Servlet规范
- Filter--URL Rewrite实现网站伪静态
- Filter的使用(使用URL Rewrite实现网站的伪静态)
- 使用PHP模拟 URL Rewrite
- URL Rewrite(2):使用组件进行URL Rewrite
- EhLib使用全攻略
- ehlib使用全攻略
- 《ANT使用全攻略》
- 车窗使用全攻略
- WebView使用全攻略
- Url Rewrite
- URL Rewrite
- URL rewrite
- 对InstallSh...的使用建议
- ajax control toolkit2005的使用方法
- JSP开发中Cookie使用完全详解
- Fileupload使用小结
- C++模板类声明与实现
- Url Rewrite Filter 使用全攻略
- 冒烟测试与BVT测试
- HLSL error X3025的问题
- 软件开发有前途吗?
- JavaScript继承详解(一)
- 3D绘图过程及原理简介
- java面试笔试题汇总一(持续更新中)
- 服务基础构架和待配信息介绍
- 结构体的字节对齐问题