http://blog.csdn.net/mr_tank_/article/details/11892965

URL重写的目的不言而喻,首先引入urlrewrite-4.0.0.jar【或者其他版本】包,可以从官方下载。

1、web.xml【官方配置】

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <display-name></display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.jsp</welcome-file>
  9. </welcome-file-list>
  10. <!-- URL重写配置 -->
  11. <filter>
  12. <filter-name>UrlRewriteFilter</filter-name>
  13. <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  14. <init-param>
  15. <param-name>logLevel</param-name>
  16. <param-value>WARN</param-value>
  17. </init-param>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>UrlRewriteFilter</filter-name>
  21. <url-pattern>/*</url-pattern><!-- 拦截所有URL -->
  22. </filter-mapping>
  23. </web-app>
<?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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</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] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
  3. "http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
  4. <!--
  5. Configuration file for UrlRewriteFilter
  6. http://tuckey.org/urlrewrite/
  7. -->
  8. <urlrewrite>
  9. <!--自定义匹配-->
  10. <rule>
  11. <!--  <from>^/admin/(.*)(.*)</from>   -->
  12. <from>admin/([0-9]+)/(.*).shtml/(.*)</from>
  13. <to>/admin_login.jsp?id=$1&name=$2&keyword=$3</to>
  14. </rule>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<!--
Configuration file for UrlRewriteFilter
http://tuckey.org/urlrewrite/
-->
<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 plaincopyprint?
  1. <!-- 官方示例-->
  2. <rule>
  3. <note>
  4. The rule means that requests to /test/status/ will be redirected to /rewrite-status
  5. the url will be rewritten.
  6. </note>
  7. <from>/test/status/</from>
  8. <to type="redirect">%{context-path}/rewrite-status</to>
  9. </rule>
  10. <outbound-rule>
  11. <note>
  12. The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
  13. the url /rewrite-status will be rewritten to /test/status/.
  14. The above rule and this outbound-rule means that end users should never see the
  15. url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
  16. in your pages.
  17. </note>
  18. <from>/rewrite-status</from>
  19. <to>/test/status/</to>
  20. </outbound-rule>
  21. <!--
  22. INSTALLATION
  23. in your web.xml add...
  24. <filter>
  25. <filter-name>UrlRewriteFilter</filter-name>
  26. <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  27. <init-param>
  28. <param-name>logLevel</param-name>
  29. <param-value>WARN</param-value>
  30. </init-param>
  31. </filter>
  32. <filter-mapping>
  33. <filter-name>UrlRewriteFilter</filter-name>
  34. <url-pattern>/*</url-pattern>
  35. </filter-mapping>
  36. EXAMPLES
  37. Redirect one url
  38. <rule>
  39. <from>/some/old/page.html</from>
  40. <to type="redirect">/very/new/page.html</to>
  41. </rule>
  42. Redirect a directory
  43. <rule>
  44. <from>/some/olddir/(.*)</from>
  45. <to type="redirect">/very/newdir/$1</to>
  46. </rule>
  47. Clean a url
  48. <rule>
  49. <from>/products/([0-9]+)</from>
  50. <to>/products/index.jsp?product_id=$1</to>
  51. </rule>
  52. eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
  53. Browser detection//浏览器检测
  54. <rule>
  55. <condition name="user-agent">Mozilla/[1-4]</condition>
  56. <from>/some/page.html</from>
  57. <to>/some/page-for-old-browsers.html</to>
  58. </rule>
  59. eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
  60. browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.
  61. Centralised browser detection
  62. <rule>
  63. <condition name="user-agent">Mozilla/[1-4]</condition>
  64. <set type="request" name="browser">moz</set>
  65. </rule>
  66. eg, all requests will be checked against the condition and if matched
  67. request.setAttribute("browser", "moz") will be called.
  68. -->
  69. </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>

3、项目结构:

4、admin_login.jsp页面代码:

[html] view plaincopyprint?
  1. <body>
  2. Admin Login Page.
  3. <br>
  4. <%=request.getParameter("id")%><br>
  5. <%=request.getParameter("name")%><br>
  6. <%=request.getParameter("keyword")%>
  7. </body>
<body>
Admin Login Page.
<br>
<%=request.getParameter("id")%><br>
<%=request.getParameter("name")%><br>
<%=request.getParameter("keyword")%>
</body>

测试结果:

http://123.125.115.53/view/1002788.html?fromTaglist

URL重写就是首先获得一个进入的URL请求然后把它重新写成网站可以处理的另一个URL的过程。举个例子来说,如果通过浏览器进来的URL是“UserProfile.aspx?ID=1”那么它可以被重写成 “UserProfile/1.aspx”,这样的URL,这样的网址可以更好的被网站所阅读。
如果浏览器不支持Cookie或用户阻止了所有Cookie,可以把会话ID附加在HTML页面中所有的URL上,这些页面作为响应发送给客户。这样,当用户单击URL时,会话ID被自动作为请求行的一部分而不是作为头行发送回服务器。这种方法称为URL重写(URL rewriting)。
一般来说,URL重写是支持会话的非常健壮的方法。在不能确定浏览器是否支持Cookie的情况下应该使用这种方法。然而,使用URL重写应该注意下面几点:
1.如果使用URL重写,应该在应用程序的所有页面中,对所有的URL编码,包括所有的超链接和表单的action属性值。
2.应用程序的所有的页面都应该是动态的。因为不同的用户具有不同的会话ID,因此在静态HTML页面中无法在URL上附加会话ID。
3.所有静态的HTML页面必须通过Servlet运行,在它将页面发送给客户时会重写URL。

JSP URL重写-urlrewrite相关推荐

  1. tp3 普通模式url模式_[tp3.2.1]开启URL(重写模式),省略URL中的index.php

    重写模式(省略url中的index.php) 在apache配置文件httpd.conf中,查找 1.mod_rewrite.so, 启动此模块 2.AllowOverride , 值= All 3. ...

  2. UrlRewrite(URL重写)--ASP.NET中的实现

    概述 今天看了下URL重写的实现,主要看的是MS 的URL Rewrite. URL重写的优点有:更友好的URL,支持老版本的URL URL重写的缺点有:最主要的缺点是性能低下,因为如果要支持无后缀的 ...

  3. UrlRewrite(Url重写技术)

    ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置------转载 ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置 核心提示:大家一定经常在网络上看到很多网站的 ...

  4. url重写(urlrewrite)的一些系统变量

    学php也有3年了,一直对url重写不是很了解,本学用到的话都是百度一下,再复制作简单修改,一些变量的参数都不太了解什么意思,难得今天有时间,做个笔记吧! 1)可用的一些系统变量,在重写条件和重写规则 ...

  5. java 重写url_网站URL重写(Java UrlRewrite 的使用)

    现在大部分的网站和商城都会使用到URL重写,接触到这个,也是因为正在做的电子商务商城.URL重写,是将原有的URL采用另一种规则来显示,使得用户方便访问同时也屏蔽一些信息. 在此说下它的好处,在开发过 ...

  6. windows2008(64位)下iis7.5中的url伪静态化重写(urlrewrite)

    以前在windows2003里,使用的是iis6.0,那时常使用的URL重写组件是iisrewrite,当服务器升级到windows2008R2时,IIS成了64位的7.5,结果iisreite组件是 ...

  7. ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置

    核心提示:大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有 ...

  8. java urlrewriter_java url重写技术(UrlReWriter)

    现在url重写技术用的越来越广泛了,很高兴今天和各位网友分享一下: 首先我们要从网上下一个UrlReWriter jar包 现在地址进不了,我这里有一个3.0版本的,文章底有下载地址,大家可以去下载 ...

  9. 《转》java URL重写

    文章来源:http://www.it300.com/article-5319.html  现在url重写技术用的越来越广泛了,很高兴今天和各位网友分享一下: 首先我们要从网上下一个UrlReWrite ...

最新文章

  1. GraphSAGE: GCN落地必读论文
  2. 批处理(定时器) ssm spring-task
  3. 解析Objective-C中多态、动态类型和动态绑定
  4. LInux 安全测试 2
  5. Python—— *与** 参数说明
  6. javafx 浏览器_浏览器中的JavaFX
  7. 农历php,php阳历转农历优化版
  8. springboot和vue data数据为空_常见数据结构的 Python 实现(建议收藏)
  9. Flutter — 实现验证码倒计时功能
  10. C++ 派生类和virtual
  11. python 03day上--网络基础及dos命令
  12. linux自学第二天
  13. 参考 雷霄骅https://blog.csdn.net/leixiaohua1020/article/list/28
  14. 密码学【java】初探究加密方式之非对称加密
  15. 飞利浦e570有JAVA吗_飞利浦E570不仅仅是手机还是一款可通话的移动电源
  16. 减少参数!SPViT:视觉Transformer剪枝新方法
  17. 人工智能与大数据行业的兴起,主要产生了哪些新的职位?
  18. C语言和JAVA的关联
  19. 【59元 第二件1元】SIMEITOL/姿美堂玛卡片 蓝玛咖片MACA 60片
  20. GO--redis redigo hel 删除多个参数的传值方法

热门文章

  1. 【数据结构与算法】之深入解析“正则表达式匹配”的求解思路与算法示例
  2. 【数据结构与算法】之深入解析如何确定单链表有环并求环的入口和长度
  3. Python中lambda表达式的优缺点及使用场景
  4. PAT (Basic Level) Practice (中文)1006 换个格式输出整数 (15 分)
  5. python 读取word 题库_Python-docx 读取word.docx内容
  6. socket连接时间太长受什么原因影响?_真全粮多功能酿酒设备-酿酒发酵前期为什么要通风处理?...
  7. java ssm使用存储过程_java调用oracle存储过程(ssm)
  8. 推箱子java下载_Java实现简单推箱子游戏
  9. 新疆大学(新大)OJ xju 1010: 四个年级 C++ STL map 将4层循环优化成2层循环可解
  10. Xcode 不用签名编译程序