用户登录
用户注册

分享至

JSP URL重写-urlrewrite

  • 作者: 用户61402477
  • 来源: 51数据库
  • 2021-09-03
url重写的目的不言而喻,首先引入urlrewrite-4.0.0.jar【或者其他版本】包,可以从官方下载。

 

1、web.xml【官方配置】

 

[html]  

<?xml version="1.0" encoding="utf-8"?>  

<web-app version="2.5" xmlns="http://www.51sjk.com/Upload/Articles/1/0/250/250749_20210625002010700.jpg  

    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"  

    xsi:schemalocation="http://www.51sjk.com/Upload/Articles/1/0/250/250749_20210625002011653.jpg   

    http://www.51sjk.com/Upload/Articles/1/0/250/250749_20210625002011653.jpg/web-app_2_5.xsd">  

    <display-name></display-name>  

    <welcome-file-list>  

        <welcome-file>index.</welcome-file>  

    </welcome-file-list>  

  

    <!-- url重写配置 -->  

    <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><!-- 拦截所有url -->  

    </filter-mapping>  

</web-app>  

 

2、urlrewrite.xml

[html]  

<?xml version="1.0" encoding="utf-8"?>  

<!doctype urlrewrite public "-//tuckey.org//dtd urlrewrite 3.2//en"  

        "https://tuckey.org/res/dtds/urlrewrite3.2.dtd">  

  

<!--  

  

    configuration file for urlrewritefilter  

    http://www.51sjk.com/Upload/Articles/1/0/250/250749_20210625002012653.jpg  

  

-->  

<urlrewrite>  

     <!--自定义匹配-->  

     <rule>     

        <!--  <from>^/admin/(.*)(.*)</from>   -->  

        <from>admin/([0-9]+)/(.*).shtml/(.*)</from>  

        <to>/admin_login.jsp?id=$1&name=$2&keyword=$3</to>    

    </rule>     

[html] view plaincopy

    <!-- 官方示例-->  

    <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>  

 

软件
前端设计
程序设计
Java相关