日期:2014-05-16  浏览次数:20335 次

jsp 用urlrewrite 实现URL 重写


下面是使用说明:

1.下载urlrewrite,官方下载地址:http://tuckey.org/urlrewrite/dist/urlrewritefilter-2.6.zip

2.解压缩文件,压缩包内文件copy到项目中(压缩包位置 -> 项目位置):

urlrewrite-2.6.0-src/webapp/WEB-INF/lib/urlrewrite-2.6.0.jar -> WebRoot/WEB-INF/lib/urlrewrite-2.6.0.jar

urlrewrite-2.6.0-src/webapp/WEB-INF/urlrewrite.xml -> WebRoot/WEB-INF/urlrewrite.xml
3.将以下代码添加到web.xml里

<filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>
            org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

?4.修改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">

<!--

    Configuration file for UrlRewriteFilter
    http://tuckey.org/urlrewrite/

-->
<urlrewrite>
	<!-- 不带参数 -->
    <rule>
        <from>^/way1</from>
        <to >/notParam.jsp</to>
    </rule>
    
    <!-- 带参数单个 -->
    <rule>
        <from>^/way2([0-9]+)$</from>
        <to >/onlyParam.jsp?id=$1</to>
    </rule>
    
    <!-- 带参数多个 -->
    <rule>
        <from>^/way3([0-9]+)/([a-z]+)/([0-9a-zA-Z]+)$</from>
        <to >/manyParam.jsp\?id=$1&amp;name1=$2&amp;name2=$3</to>
    </rule>
</urlrewrite>

?

?

5、建立index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>URL 重写</title>
	 <script type="text/javascript">
		function formSubmit()
		{

			var way=document.getElementById("way").value;
			if(way==1){
				form1.action="way1"
				form1.submit();
			}
			if(way==2){
				form1.action="way2"+way
				form1.submit();
			}
			if(way==3){
				form1.action="way3"+way+"/admin"+"/admin110"
				form1.submit();
			}
		}
	</script>
  </head>
  
  <body>
  <form name="form1" id="form1"  method="post">
  	<h1>URL 重写</h1>
  	<select name="way" id="way">
  		<option value="1">不带参数的URL</option>
  		<option value="2">带参数的URL(单个参数)</option>
  		<option value="3">带参数的URL(多个参数)</option>
  	</select>
  	<input type="button" value="提交" onclick="formSubmit()"/> 
  </form>
  </body>
</html>

??

注意:这里action提交的是way1

6、建立notParam.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
  <head>
 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无参数</title>
  </head&