http://blog.csdn.net/mr_tank_/article/details/11892965
URL重写的目的不言而喻,首先引入urlrewrite-4.0.0.jar【或者其他版本】包,可以从官方下载。
1、web.xml【官方配置】
- <?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>
<?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
- <?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>
<?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>
- <!-- 官方示例-->
- <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>
<!-- 官方示例--> <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页面代码:
- <body>
- Admin Login Page.
- <br>
- <%=request.getParameter("id")%><br>
- <%=request.getParameter("name")%><br>
- <%=request.getParameter("keyword")%>
- </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,这样的网址可以更好的被网站所阅读。
“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。