urlrewrite地址重寫的使用

UrlRewrite就是我們通常說的地址重寫,用戶得到的全部都是經過處理後的URL地址。

優點:

一:提高安全性,可以有效的避免一些參數名、ID等完全暴露在用戶面前,如果用戶隨便亂輸的話,不符合規則的話直接會返回個404或錯誤頁面,這比直接返回500或一大堆服務器錯誤信息要好的多

二:美化URL,去除了那些比如*.do之類的後綴名、長長的參數串等,可以自己組織精簡更能反映訪問模塊內容的URL、

三:更有利於搜索引擎的收入,通過對URL的一些優化,可以使搜索引擎更好的識別與收錄網站的信息

使用範圍:

地址重寫一般是用於將動態地址僞靜態。如果本身就是靜態就沒必要了。地址重寫後網站製作者可以通過輸入地址名直接訪問。

【第一步】下載jar包

在urlrewrite官方網站 http://tuckey.org/urlrewrite/ 下載最新的Jar包

【第二步】新建Web項目TestUrlRewrite

在WebRoot下新建index.jsp,修改其代碼如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TestUrlRewrite</title>
</head>
<body>
	<%
		out.println("<h3> 永遠不要低估一個女生和你同甘共苦的決心。但也別忘記,一個女生最怕的,是在你身上看不到希望。 </h3> ");
	%>
</body>
</html>
【第三步】添加urlrewrite文件

將urlrewrite.jar複製到WebRoot\WEB-INF\lib下。(注意:這個配置文件一定只能放在 WEB-INF 下

將urlrewrite.xml複製到WebRoot\WEB-INF下,其中urlrewrite.xml,修改其代碼如下所示:

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN" "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
	<rule>
		<note>重寫index.jsp爲index.html</note>
		<from>index.html</from> <!-- 定義具體的匹配規則 -->
		<to>/index.jsp</to>
	</rule>

	<rule>
		<note><span style="line-height: 21px; white-space: pre-wrap;">example: /index/1.html</note></span>
		<from>/(.*)/([0-9]+).html</from> <!-- 定義具體的匹配規則 -->
		<to type="forward">/$1.jsp?id=$2</to> <!-- 匹配成功後的目標地址 -->
		<!--注 $1:匹配中的第一個正則表達式的字符串的值,$2,$3,$4....也是如此 -->
	</rule>

	<rule>
		<note>redirect(重定向模式)傳參,to中寫絕對地址</note>
		<note>example:/index/amdin/pass.do</note>
		<from>/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+).do</from>
		<to type="redirect">/$1.jsp?username=$2&password=$3</to>
	</rule>
</urlrewrite>


參數詳解:

<rule>:自定義匹配規則。

<note>:註釋,解釋標籤。

<from>:定義具體的匹配規則。

<to>:匹配成功後的目標地址。

<to type="">:type的值有兩個,一個是 forward (轉發,參數不丟失),一個是 redirect (重定向,地址欄顯示的地址就是目標真實地址)

$1:匹配中的第一個正則表達式的字符串的值,$2,$3,$4....也是如此。

&amp; :是 & 的實體名,代表的就是 &。

【第四步】修改web.xml文件

WEB-INF中添加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">
	<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>DEBUG</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>UrlRewriteFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
【第五步】測試

實例一:重寫index.jsp爲index.html



實例二:forward轉發模式傳參

(1)index.jsp頁面代碼


(2)在瀏覽器中輸入 “ http://localhost:8080/index/12.html ”


實例三:redirect(重定向模式)傳參,to中寫絕對地址

(1)index.jsp頁面代碼,如下截圖


(2)在瀏覽器中輸入 “ localhost:8080/index/admin/pass.do ”





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章