Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单,老少皆宜。
估计大多数朋友都是直接配置用,也没有关心过具体的细节,今天遇到一个问题,上网查了servlet的规范才发现,servlet和filter中的url-pattern还是有一些文章在里面的,总结了一些东西,放出来供大家参考,以免遇到问题又要浪费时间。
一、servlet容器对url的匹配过程:
当一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是 http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉, 剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下 的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下:
1. 精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的 servlet了。
2. 最长路径匹配。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此 时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB.
3. 扩展匹配,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet.例子:servletA的url-pattern:*.action
4. 如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个default servlet,则容器会将请求丢给default servlet(什么是default servlet?后面会讲)。
根据这个规则表,就能很清楚的知道servlet的匹配过程,所以定义servlet的时候也要考虑url-pattern的写法,以免出错。
对于filter,不会像servlet那样只匹配一个servlet,因为filter的集合是一个链,所以只会有处理的顺序不同,而不会出现只选择一个filter.Filter的处理顺序和filter-mapping在web.xml中定义的顺序相同。
二、url-pattern详解
在web.xml文件中,以下语法用于定义映射:
以“/‘开头和以”/*“结尾的是用来做路径映射的。
以前缀“*.”开头的是用来做扩展映射的。
“是用来定义default servlet映射的。
剩下的都是用来定义详细映射的。比如: /aa/bb/cc.action
所以,为什么定义“/*.action”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断。
相关推荐
Linux的作者Linus Torvalds的C语言函数库源代码(部分)string.c<br><br>1 /*<br>2 * linux/lib/string.c<br>3 *<br>4 * Copyright (C) 1991, 1992 Linus Torvalds<br>5 */<br>6 <br>7 /*<br>8 * stupid library ...
7、crud 增删改查 搜索 多删 假删 ids ->action -> dao PreparedStatement -- "delete from user where id in ("+ids+")"; 8、converter i18n ModeDriven stack 9、interceptor token 5、hibernate 4.1.4 ...
解决web.xml中 <page-encoding>UTF-8</page-encoding>报错。错误提示: cvc-complex-type.2.4.a: Invalid content was found starting with element 'page-encoding'. One of '{"http:// java.sun....
entry’:/data/src/php-5.6.23/ext/openssl/openssl.c:664: warning: ‘ASN1_STRING_data’ is deprecated (declared at /usr/local/include/openssl/asn1.h:553)/data/src/php-5.6.23/ext/openssl/openssl.c: In ...
gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-guneabi.tar.xz gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-guneabi.tar.xz gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-guneabi.tar.xz gcc-linaro-4.9.4-2017.01-x86_...
/// <summary> /// 板卡类型 /// </summary> public enum BOARD_TYPE_DS : uint { DS400XM = 0, //M卡 DS400XH = 1, //H卡 DS4004HC = 2, //4004HC DS4008HC = 3, //4008HC DS4016HC = 4, //4016HC DS...
xjar的4.0.1版本,便于加密gui开发时引用.访问jitpack.io 仓库 失败的可以下载,然后通过maven命令安装到本地仓库,直接应用即可. ...<artifactId>xjar</artifactId> <version>4.0.1</version> </dependency>
#include <assert.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #include <limits.h> /* use configuration data */ #include <tomcrypt_custom.h> #...
The 80x86 MOV Instruction<br>4.8 - Some Final Comments on the MOV Instructions<br><br>4.9 Laboratory Exercises<br>4.9.1 The UCR Standard Library for 80x86 Assembly Language Programmers<br>4.9.2 ...
<br>}<br></script><br></head><br><body><br><form id="form1" runat="server"><br><asp:ScriptManager ID="ScriptManager1" runat="server"><br><Services><br><asp:ServiceReference Path="~/WebServices/...
<?xml version="1.0" encoding="UTF-8"?> <license> <terms> <issued_to>rapidhorse.com.cn</issued_to> <license_identifier>SN732563653</license_identifier> <edition>enterprise</edition> <evaluation>N</...
ubuntu20.04 /lib/x86_64-linux-gnu /libm.so.6
#include <netinet/in.h> #include <arpa/inet.h> /* inet_ntoa */ #include <errno.h> #include <inttypes.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket close #define ...
Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...
解决 cvc-complex-type.2.4.d: Invalid content was found 具体参考http://henatne.iteye.com/blog/837186
rootroot@rootroot-E400:~/wyb/ap6212a0_a33_sc3817r/lichee$ ./build.sh config Welcome to mkscript setup progress All available chips: 0. sun8iw5p1 Choice: 0 All available platforms: 0. android 1. ...
离线安装包,亲测可用
MMU - maps CPU virtual addresses into physical addresses, for valid mappings<br> TRAP - an interruption in the normal sequencing of a CPU due to an invalid<br> memory mapping, a trap, exception, or an...
被我删除的nvidia-kernel 博文链接:https://qianjigui.iteye.com/blog/294719
/* 1 INVALID FILTER ORDER (L<=0 OR L>=LN) */ /* 2 INVALID SCALE PARAMETER (WSCL<=0) */ #ifndef KR void spbssl(long *l, float *wscl, long *ln, float *d, float *c, long *error) #else void spbssl(l, ...