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="提交" οnclick="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>
  
  <body>
    无参数
  </body>
</html>

 7、建立onlyParam.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>
  
  <body>
    id:<%=request.getParameter("id") %>
  </body>
</html>

 8、建立manyParam.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>
  
  <body>
  	<h2>数字:<%=request.getParameter("id") %></h2>
    <h2>英文:<%=request.getParameter("name1") %></h2>
    <h2>数字与英文:<%=request.getParameter("name2") %></h2>
  </body>
</html>

  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值