现在url重写技术用的越来越广泛了,很高兴今天和各位网友分享一下:
首先我们要从网上下一个UrlReWriter jar包 现在地址进不了,我这里有一个3.0版本的,文章底有下载地址,大家可以去下载
引用jar包后,我们需要配制web.xml配制文件,配制如下:
01
02
UrlRewriteFilter
03
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
04
05
06
UrlRewriteFilter
07
/*
08
REQUEST
09
FORWARD
10
新建一个urlrewrite.xml文件
01
02
03
04
05
/test.html
06
%{context-path}/page.html
07
08
09
/name/(.*)
10
/MyName.jsp?name=$1
11
12
我们再新建test.html
01
1
02
2
03
3
04
4
05
5
Insert title here06
6
07
7
08
8 test.html
09
9
10
10
新建 page.html
01
1
02
2
03
3
04
4
05
5
Insert title here06
6
07
7
08
8 page.html
09
9
10
10
新建MyName.jsp
01
1
02
2 pageEncoding="UTF-8"%>
03
3
04
4
05
5
06
6
07
7
MyName08
8
09
9
10
10
11
11 我的姓名是:
12
12
13
13
我们实现的效果是输入http://localhost:8080/MyUrl/name/chenjie 达到效果http://localhost:8080/MyUrl/MyName.jsp?name=chenjie
输入http://localhost:8080/MyUrl/test.html 跳转到http://localhost:8080/MyUrl/page.html
URL重写我们最重要是正则表达式的写法,我们随便也介绍一下正则的用法
查看源码打印
01
1 package com.testMatch;
02
2
03
3 import java.util.regex.Matcher;
04
4 import java.util.regex.Pattern;
05
5 public class MyMatch {
06
6 public static void main(String[] args) {
07
7 /** 方法一
08
8 //创建pattern对象,加载正则表达式
09
9 Pattern p=Pattern.compile("^t.*java");
10
10 //将需要的匹配的字符串加载到Matcher对象、
11
11 Matcher m=p.matcher("think in java");
12
12 boolean b=m.matches();
13
13 System.out.println(b);
14
14 **/
15
15 //".代表一任意一个字符"
16
16 //"^"1.在字符串中代表开始 2.在[]代表取反
17
17 //"$"表示结尾
18
18 //"|"表示或者,^[a|b]表示以a或b开头^[1][358]
19
19 //"[]"表示选择,匹配手机号码:^[1][358][0-9]{9}
20
20 //"*"表示前面的字符可以出现0次,1次,或多次
21
21 //"+"表示前面的字符至少出现一次或多次
22
22 //"?"表示前面的字符可以出现0或1次
23
23 //"{}"允许前面的字符可以出现多少次
24
24 boolean b=Pattern.matches("^t.*java","think in java");
25
25
26
26
27
27
28
28 }
29
29 }
本文有麦克疯下载:http://51maike.sinaapp.com/ 提供
人打赏
0人 点赞
主帖获得的天涯分:0
举报 |
楼主
|
楼主发言:1次 发图:0张 | 添加到话题 |