伪静态URLRewrite学习笔记

来源:互联网 发布:数据挖掘看什么书 编辑:程序博客网 时间:2024/05/01 05:43

UrlRewrite

UrlRewrite就是我们通常说的地址重写,用户得到的全部都是经过处理后的URL地址,类似于Apachemod_rewrite。将我们的动态网页地址转化为静态的地址,如htmlshtml,还可以隐藏网页的真正路径,

比如:有时候需要将xxx.com/news/ type1/001.jsp 转化成显示路径为xxx.com/news_type1_001.html


有点如下:

一:提高安全性,屏蔽内部的url结构.

二:美化URL

三:更有利于搜索引擎的收入,通过对URL的一些优化,可以使搜索引擎更好的识别与收录网站的信息.


下载地址:

官网下载: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#filterparams


实例展示

实例应用版本urlrewritefilter-4.0.3. Tomcat服务器端口定制为80

1. 创建web项目,增加 urlrewritefilter-4.0.3.jar 到 WEB-INF/lib 

2. 在WEB-INF/web.xml 增加urlrewritefilter过滤器 (near the top above any servlet mappings)

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <!-- 加到任何servlet映射的顶部,不然可能有些路径不能被过滤到         http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html     -->    <filter>        <filter-name>UrlRewriteFilter</filter-name>        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>        <!--             设备文件重加载间隔 (0默示随时加载, -1默示不重加载, 默认-1)         -->        <init-param>            <param-name>confReloadCheckInterval</param-name>            <param-value>60</param-value>        </init-param>                <!-- 自定义配置文件的路径,是相对context的路径,(默认位置 /WEB-INF/urlrewrite.xml) -->        <init-param>            <param-name>confPath</param-name>            <param-value>/WEB-INF/urlrewrite.xml</param-value>        </init-param>                <!--             设置日志级别(将被记录到日志中)               可以为: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, slf4j,               比如 sysout:DEBUG(设置到控制台调试输出级别)             (默认级别 WARN) -->        <init-param>            <param-name>logLevel</param-name>            <param-value>DEBUG</param-value>        </init-param>    </filter>        <filter-mapping>        <filter-name>UrlRewriteFilter</filter-name>        <url-pattern>/*</url-pattern>        <dispatcher>REQUEST</dispatcher>        <dispatcher>FORWARD</dispatcher>    </filter-mapping>            <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

如果觉得/*这样的通配,并不符合我的预期,我只想对部分路径进行URL的重写,/*可能会造成我想象不到的或者是许微不足道的性能浪费.我把它改成了我需要的:

    <filter-mapping>        <filter-name>UrlRewriteFilter</filter-name>        <url-pattern>/member/*</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>UrlRewriteFilter</filter-name>        <url-pattern>/article/*</url-pattern>    </filter-mapping>

更多请参考: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.2/index.html

3. 因为上面我们通过confPath定义了配置文件的路径,其实该默认位置就是在/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"><urlrewrite>    <rule>        <from>/page/(.*).html</from>          <to>/index.jsp?page=$1</to>     </rule>        <rule>        <from>^/user/([a-z]+)/([0-9]+)$</from>        <to>/index.jsp?nickname=$1&amp;age=$2</to>  </rule></urlrewrite>

此时我们就可以通过url进行模拟了.

注意:

1.urlrewrite.xmlutf-8.所以如果你要在rule上加note标签为中文的话,也一定是要utf-8.

2.UrlRewriteFilter 最好是配置在web.xml的前面filter,不然有可能对有些url转变失去作用.

3.urlrewrite属性:有仅只有一个,rule属性::至少一个.

4.在写rule的时,如果有多个参数时,中间的连接符号&应该是&

5.ruleurl重写规则,from是显示出来的地址,to是映射的实际地址,$1是重写参数,它的值与from中的正则表达式是一一对应,可以为多个,()里是匹配的正则表达式在正则表达式^指定字符的串开始,$为指定结束

6.对于中文参数要使用(.*)作为参数转义.

4.重写url演示

实例1

<rule>        <from>/page/(.*).html</from>          <to>/index.jsp?currentPage=$1</to> </rule>

index.jsp中的内容

  <body>          <%              String current = request.getParameter("currentPage");           %>                    当前页码<%=current %>  </body>

执行效果如下:



实例2

Rule规则

<rule>        <name>World Rule</name>        <from>^/user/([a-z]+)/([0-9]+)$</from>        <to>/index.jsp?nickname=$1&amp;age=$2</to></rule>

index.jsp中的内容

<body>          <%              String username = request.getParameter("nickname");              int age = Integer.parseInt(request.getParameter("age"));           %>                    用户名: <%=username %> 年龄: <%=age %> <br></body>

执行效果如下:


所以,当我们在url中输入”http://localhost/urlrewrite/user/dennisit/23”时,实际执行的就是”http://localhost/urlrewrite/index.jsp?nickname=dennisit&age=23”

实例3

同理rule规则如下时

<rule>        <from>^/page/(.*)$</from>        <to type="redirect">/page/$1.action</to></rule>

这样我访问的:http://localhost/urlrewrite/page/test

则跳转到:    http://localhost/urlrewrite/page/test.action


实例4
Rule规则

    <rule>        <from>^/([a-z]+)/([a-z]+)/([a-z]+)$</from>        <to>/$1.do?method=$2&amp;uuid=$3</to>    </rule>

index.jsp中添加如下链接:

    <a href="process/show/index">跳转</a>

当点击该链接,

地址栏中显示url是:http://localhost/urlrewrite/process/show/index,

其实际执行路径是:http://localhost/urlrewrite/process.do?method=show&uuid=index

0 0