首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jsp 用urlrewrite 兑现URL 重写

2012-08-24 
jsp 用urlrewrite 实现URL 重写下面是使用说明: 1.下载urlrewrite,官方下载地址:http://tuckey.org/urlrew

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"+wayform1.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>    <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>

??

?

?

热点排行
Bad Request.