---
title: "jsp 用urlrewrite 实现URL 重写"
author: "httroot"
date: "2009-07-09T17:01:07Z"
tags: []
slug: "jsp-urlrewrite-url"
draft: false
lastmod: "2009-08-19T10:19:44Z"
id: 422738
url: "https://httroot.iteye.com/blog/422738"
---

推广： 自己开的淘宝店，主营汽车饰品、配件，大家可以进去看一下，最好是能帮哥们推广一下

店名： **轩辕车饰** 

          

```
http://shop36647698.taobao.com/
```

 

 

 

 

 

URLRewirte的用处:  
1.满足搜索引擎的要求  
2.隐藏技术实现，提高网站的移植性  
3.满足美感的要求

项目部署  
1.首先在http://tuckey.org/urlrewirte/下载urlrewirtefilter  
2.解压所下载的文件,把urlrewrite-2.6.0.jar复制到项目的WebRoot/WEB-INF/lib/目录下,然后编译  
3.把urlrewrite.xml复制到项目的WebRoot/WEB-INF/目录下  
4.在web.xml文件中加入以下:

Xml代码

```
   1. <!-- 动态地址静态化 -->  
   2. <filter>  
   3.   <filter-name>UrlRewriteFilter</filter-name>  
   4.   <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
   5.   <init-param>  
   6.     <param-name>logLevel</param-name>  
   7.     <param-value>WARN</param-value>  
   8.   </init-param>  
   9. </filter>  
  10. <filter-mapping>  
  11.   <filter-name>UrlRewriteFilter</filter-name>  
  12.   <url-pattern>/*</url-pattern>  
  13. </filter-mapping>  
```

 

```
<!-- 动态地址静态化 -->
<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>
```

5.需要配置urlrewrite.xml文件来实现url静态化,下面将详细说明

到这里相关的配置已经完成,下面看如何把动态地址静态化  
1.普通url静态化  
例如:要把http://localhost/prjtest/user/list.jsp转换成http://localhost/prjtest/user/list.html  
这种是最简单的,当一个servlet跳转到list.jsp页面列出user列表时,在urlrewrite.xml中这样配置:

Xml代码 &lt;embed type="application/x-shockwave-flash" width="14" height="15" src="http://major361.iteye.com/javascripts/syntaxhighlighter/clipboard\_new.swf" flashvars="clipboard=%3Crule%3E%0A%20%20%3Cfrom%3E%5E%2Fuser%2Flist.html%3C%2Ffrom%3E%0A%20%20%3Cto%3E%2Fuser%2Flist.jsp%3C%2Fto%3E%0A%3C%2Frule%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;&lt;/embed&gt;

```
<rule>  
   <from>^/user/list.html</from>  
   <to>/user/list.jsp</to>  
 </rule> 
```

 

```
<rule>
  <from>^/user/list.html</from>
  <to>/user/list.jsp</to>
</rule>
```

当请求/user/list.html这个页面时,实际上相当于请求/user/list.jsp页面  
在servlet的跳转要这样写:  
response.sendRedirect("./user/list.html");  
2.带参数的url静态化  
例如:要把http://localhost/prjtest/user/view.jsp?cid=1&cname=admin转换成  
       http://localhost/prjtest/user/view/1\_admin.html  
在urlrewrite.xml中这样配置:

Xml代码

1. ```
    <rule>  
      <from>^/user/view/([0-9]+)_([a-z]+).html$</from>  
      <to>/user/view.jsp?cid=$1&amp;cname=$2</to>  
    </rule> 
   ```

```
<rule>
  <from>^/user/view/([0-9]+)_([a-z]+).html$</from>
  <to>/user/view.jsp?cid=$1&amp;cname=$2</to>
</rule>
```

当请求/user/view/1\_admin.html这个页面时,实际上相当于请求/user/list.jsp?cid=1&cname=admin页面  
在servlet的跳转要这样写(cid,cname为变量):  
response.sendRedirect("./user/view/"+ cid +"\_"+ cname +".html");  
注意:配置文件中用"&amp;"来代替"&"  
一个通用的正则表达式:\[a-zA-Z0-9]+