前几天跟朋友聊天才知道有个url重写技术,他说搞web的都知道(汗...),恨不得立马找个洞钻进去,把自己埋起来。
刚接触还一头雾水,经朋友指点帮助,现在略懂一二了,呵呵,现在给大家写个实例,希望对还不懂的朋友一点帮助。
1.准备:
下载:urlrewritefilter-2.6.zip,解压,提取urlrewrite.jar,放到工程lib目录下。
2.修改web.xml
增加如下代码:
<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>
3.新建urlrewriet.xml,放到WEB-INF目录下
内容如下:
<?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>
<from>/([0-9]+)</from>
<to>/MyJsp.jsp?id=$1</to>
</rule>
</urlrewrite>
4。新建测试页MyJsp.jsp,放到根目录下
内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>ww.pjwqh.cn</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String a = request.getParameter("id");
%>
<%=basePath %> <br>
<%
if(a.equals("123"))
{
out.println("测试成功");
}
else
{
out.println("出错拉!");
}
%>
</body>
</html>
5.测试
在IE中输入http://localhost:8088/123,页面成功转到MyJsp.jsp
解释下,我们通过输入123转到MyJsp.jsp页面的功能是由
<from>/([0-9]+)</from>
<to>/MyJsp.jsp?id=$1</to>
这段配置来提供的,from则是浏览器中显示的路径,to则是通过from访问的实际路径,另外这里还提供了带参数传递,用到了正则表达式,关与正则表达式可以看这里的快速入门教程。
刚接触还一头雾水,经朋友指点帮助,现在略懂一二了,呵呵,现在给大家写个实例,希望对还不懂的朋友一点帮助。
1.准备:
下载:urlrewritefilter-2.6.zip,解压,提取urlrewrite.jar,放到工程lib目录下。
2.修改web.xml
增加如下代码:
<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>
3.新建urlrewriet.xml,放到WEB-INF目录下
内容如下:
<?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>
<from>/([0-9]+)</from>
<to>/MyJsp.jsp?id=$1</to>
</rule>
</urlrewrite>
4。新建测试页MyJsp.jsp,放到根目录下
内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>ww.pjwqh.cn</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String a = request.getParameter("id");
%>
<%=basePath %> <br>
<%
if(a.equals("123"))
{
out.println("测试成功");
}
else
{
out.println("出错拉!");
}
%>
</body>
</html>
5.测试
在IE中输入http://localhost:8088/123,页面成功转到MyJsp.jsp
解释下,我们通过输入123转到MyJsp.jsp页面的功能是由
<from>/([0-9]+)</from>
<to>/MyJsp.jsp?id=$1</to>
这段配置来提供的,from则是浏览器中显示的路径,to则是通过from访问的实际路径,另外这里还提供了带参数传递,用到了正则表达式,关与正则表达式可以看这里的快速入门教程。