URL(基础二) UrlRewriteFilter的使用

1.下载urlrewrite,官方下载地址:http://tuckey.org/urlrewrite/

2.非maven项目:将urlrewritefilter-4.0.3.jar复制到WEB-INF/lib里

如果是maven项目,添加依赖:

如:

<!--引入URL插件 -->

<dependency>
   <groupId>org.tuckey</groupId>
   <artifactId>urlrewritefilter</artifactId>
   <version>4.0.3</version>
</dependency>

3.将以下代码添加到web.xml里,路径为: /webapp/WEB-INF/web.xml

Xml代码

<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>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>


4.新建一个urlrewrite.xml,路径为:/webapp/WEB-INF/urlrewrite.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<!--
    Configuration file for UrlRewriteFilter
    http://www.tuckey.org/urlrewrite/
-->
<urlrewrite>
    <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></rule>标签是url重写规则,我们需要根据实际情况,进行修改。
    <from></from>标签是显示出来的地址,<to></to>标签是映射的实际地址
参数:$1是重写参数,可以为多个,()里是匹配的正则表达式.

如:

<rule>  
     <from>^/newsInfo-list-([0-9]+).shtml$</from>  
     <to>/NewsInfo.do?method=list&mod_id=$1</to>  
</rule>  
<outbound-rule>  
     <from>/NewsInfo.do\?method=list&mod_id=([0-9]+)$</from>  
     <to>/newsInfo-list-$1.shtml</to>  
 </outbound-rule>


5.JSP页面使用:

<c:url var="url_1001001000" value="/NewsInfo.do?method=list&mod_id=1001001000" />  
<li><a href="${url_1001001000}">测试地址</a></li>

官网文档中提供如下使用方式:

Xml代码   收藏代码
  1. Using the example above JSP's with the code   
  2. <a href="<%= response.encodeURL("/world.jsp?country=usa&amp;city=nyc") %>">nyc</a>   
  3. will output   
  4. <a href="/world/usa/nyc">nyc</a>  
  5.   
  6. Or JSTL   
  7. <a href="<c:url value="/world.jsp?country=${country}&amp;city=${city}" />">nyc</a>   
  8. will output   
  9. <a href="/world/usa/nyc">nyc</a>  
  10.   
  11. Note, If you are using JSTL (ie, <c:url) this will work also.  

6、基本原理

Xml代码   收藏代码
  1. jsp页面地址--> 服务器filter过滤 --> 调用urlrewrite.xml映射规则  
  2. -->服务器响应 --> 转换成伪地址  

 

7、在项目中新建index.jsp,启动tomcat,输入http://localhost:8080/webapp/index/1 (注:webapp是项目名称)

实际上访问的是http://localhost:8080/webapp/index.jsp?tid=1 ,这样就简单的实现了伪静态的效果
在ajax当中,var url = "";这个地方写个虚拟的访问路径,

然后将解析规则用正则表示到urlrewrite.xml当中,那么到后台的时候,就可以自动解析成真实的路径,从而达到对url的保护。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值