UrlRewrite:html
UrlRewrite就是咱们一般说的地址重写,用户获得的所有都是通过处理后的URL地址,相似于Apache的mod_rewrite。将咱们的动态网页地址转化为静态的地址,如html、shtml,还能够隐藏网页的真正路径,java
好比:有时候须要将xxx.com/news/ type1/001.jsp 转化成显示路径为xxx.com/news_type1_001.htmlweb
有点以下:正则表达式
一:提升安全性,能够有效的避免一些参数名、ID等彻底暴露在用户面前,若是用户随便乱输的话,不符合规则的话直接会返回个404或错误页面,这比直接返回500或一大堆服务器错误信息要好的多安全
二:美化URL,去除了那些好比*.do之类的后缀名、长长的参数串等,能够本身组织精简更能反映访问模块内容的URL服务器
三:更有利于搜索引擎的收入,经过对URL的一些优化,可使搜索引擎更好的识别与收录网站的信息app
下载地址:
官网下载: http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/4.0/index.html#filterparams
实例展现jsp
实例应用版本urlrewritefilter-4.0.3. Tomcat服务器端口定制为80
svn
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&age=$2</to> </rule></urlrewrite>
此时咱们就能够经过url进行模拟了.
注意:
1.urlrewrite.xml是utf-8.因此若是你要在rule上加note标签为中文的话,也必定是要utf-8.
2.UrlRewriteFilter 最好是配置在web.xml的前面filter上,否则有可能对有些url转变失去做用.
3.urlrewrite属性:有仅只有一个,rule属性::至少一个.
4.在写rule的时,若是有多个参数时,中间的链接符号&应该是&
5.rule是url重写规则,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&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&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