读书人

运用Url Rewrite Filter实现页面伪静态

发布时间: 2012-10-30 16:13:35 作者: rapoo

使用Url Rewrite Filter实现页面伪静态化

Url Rewrite Filter 是一个基于java的 url rewirte开源项目,功能类似Apache中的rewrite模块<o:p></o:p>

一.???? 主要功能<o:p></o:p>

a)???????? 实现url地址的伪静态化<o:p></o:p>

例如可以把http://localhost:8080/myindex.do?method=listIndex&user=ezplus的请求重定向或转发到http://localhost:8080/blog/ezplus,这样做就避免了url中出现?&之类的符号,而且页面伪静态化之后也增加了被搜索引擎找到的概率.<o:p></o:p>

b)???????? 根据配置文件自动转化页面上的连接<o:p></o:p>

在写页面的时候程序员不用关心在页面中出现的url地址将被如何伪静态化,例如,程序员还是可以在写页面的时候使用http://localhost:8080/myindex.do?method=listIndex&user=ezplus,? 通过在urlrewrite.xml中配置<outbound-rule></outbound-rule>,既可以实现页面url地址自动转化为伪静态化后的地址,用户在查看页面源码的时候原http://localhost:8080/blog/ezplus将被自动替换为http://localhost:8080/blog/ezplus<o:p></o:p>

二.???? 其他功能<o:p></o:p>

……<o:p></o:p>

三.???? 安装使用<o:p></o:p>

a)???????? 下载和安装 Tomcat<o:p></o:p>

b)???????? 部署你的应用<demo app=""></demo><o:p></o:p>

c)???????? 下载? Url Rewrite Filter http://tuckey.org/urlrewrite/<o:p></o:p>

d)???????? 解压 下载后的 urlrewritefilter-3.0.zip 到 <tomcat_home></tomcat_home>\webapps\<demo app=""></demo><o:p></o:p>

e)???????? 修改 web.xml 如下<o:p></o:p>

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app id=”WebApp_9″ version=”2.4″ xmlns=”http://java.sun.com/xml/ns/j2ee”
???????? xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
???????? xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“><o:p></o:p>

<display-name>Url Rewrite</display-name><o:p></o:p>

<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>
???</web-app><o:p></o:p>

f)?????????? 修改 WEB-INF 下的 urlrewrite.xml:<o:p></o:p>

<rule></rule><o:p></o:p>

??????? <from></from>/blog/([0-9]+)<o:p></o:p>

??????? <to></to>/myindex.do?method=listIndex&user=$1<o:p></o:p>

<o:p></o:p>

<outbound-rule></outbound-rule><o:p></o:p>

????? ???<from></from>/myindex.do\?method=listIndex&user=([0-9]+)<o:p></o:p>

??????????? ?<!---->(request.getContextPath()+"/myindex.do?method=listIndex&user=ezplus)%>这里我在”?”前加了转义符”/”,但是看网上很多文章上都不用加,我试过不加,但是总是报错,不知道为什么?? --><o:p></o:p>

????????? <to></to>/blog/$1<o:p></o:p>

?<o:p></o:p>

8.? jsp页面中原来使用http://localhost:8080/myindex.do?method=listIndex&user=ezplus的地方现改为<o:p></o:p>

response.encodeURL

这里一定要使用response.encodeURL否则<outbound-rule></outbound-rule>不起作用<o:p></o:p>

也可以使用jstl中的<c:url></c:url>标签

?

?

1 楼 海妖的夜 2007-05-20 这个让apache来负责才是最好的选择吧?
mod_rewrite已经很成熟了,功能,或者效率上都更好吧? 2 楼 zhyun29 2007-05-20 没错,的确如此,用apache就彻底把urlrewrite的功能透明了,对于多个应用使用起来更方便。urlrewrite filter只是给那些不打算使用apache的应用提供了另外一种选择。 3 楼 galaxystar 2007-05-20 二楼说得不错,apache就是做这个的,干嘛要分离它的职责! 4 楼 mmwy 2007-05-20 存在的就是合理的。


另:url_rewtirer手册上有这么一个condition:session-attribute,请问楼上的,不用url rewriter filter,如何用mod_rewrite实现?
5 楼 itjunhua 2007-08-17 apache的效率可能会高些 6 楼 sprite 2007-08-17 在项目里使用过Url Rewrite Filter,实现页面伪静态化确实挺不错的,可以将地址栏伪装成目录结构从而可以增加搜索引擎的搜索度(因为文件夹的搜索系数比页面的高),也可以借助apache的VirtualHost 在没有开通泛域名解析的情况下实现二级域名,像这样<font><a href='http://xiaoqu.goojia.com/'>http://xiaoqu.goojia.com/</a>???????????<a href='http://xiaoqu.goojia.com/xiaoqu-2434'><font>http://xiaoqu.goojia.com/xiaoqu-2434</font></a>????? <font><a href='http://xiaoqu.goojia.com/search/--%E9%97%B5%E8%A1%8C%E5%8C%BA-%E5%8F%A4%E7%BE%8E%E7%BD%97%E9%98%B3%E6%9D%BF%E5%9D%97-----2-0------------price.html'>http://xiaoqu.goojia.com/search/--%E9%97%B5%E8%A1%8C%E5%8C%BA-%E5%8F%A4%E7%BE%8E%E7%BD%97%E9%98%B3%E6%9D%BF%E5%9D%97-----2-0------------price.html</a></font>但是值得注意的一点是,因为请求进出都有拦截,所以性能方面不是太好。</font> 7 楼 wl1985 2007-08-19 appfuse就用了楼主所说的rewriteFilter
配置如下
filter>
<filter-name>rewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>commons</param-value>
</init-param>
</filter> 8 楼 farush 2007-08-19 <p>zhyun29 写道</p>
<p><!--/stro-->(request.getContextPath()+"/myindex.do?method=listIndex&user=ezplus)%><strong><span style='font-size: 9pt;'><font>这里我在</font></span></strong><strong><span lang='EN-US' style='font-size: 9pt; font-family: Arial;'>”?”</span></strong><strong><span style='font-size: 9pt;'><font>前加了转义符</font></span></strong><strong><span lang='EN-US' style='font-size: 9pt; font-family: Arial;'>”/”,</span></strong><strong><span style='font-size: 9pt;'><font>但是看网上很多文章上都不用加,我试过不加,但是总是报错,不知道为什么?</font></span></strong><strong><span lang='EN-US' style='font-size: 9pt; font-family: Arial;'><span>? </span>--></span></strong></p>
<p><strong><span lang='EN-US' style='font-size: 9pt; font-family: Arial;'><o:p></o:p></span></strong></p>
<p>?</p>
<p>因为:</p>
<p>request.getContextPath() 得到的是/myapp 这样的路径,而不是/myapp/</p>
<p>?</p> 9 楼 blowfisher 2007-08-19 使用 UrlRewriteFilter 的一个好处是,在做web前端设计的时候,就可以直接使用规划好的URLs,不用一直拖上一个 APACHE 来测试 URLs 的可用性。<br />
<br />
等到集成测试、布署的时候,考虑性能等因素,再把UrlRewriteFilter拿掉,换上apache的 rewrite 。<br />
<br />
如果不是非常大型的项目,UrlRewriteFilter 并没有显著的性能影响,比起中间层和持久层的性能损失,这点几乎可以忽略 10 楼 freesea 2008-05-02 最近在应用的时候才知道使用这个会严重影响了性能,请看

http://www.iteye.com/post/533819

读书人网 >软件架构设计

热点推荐