JavaWeb开发中,servlet的url-pattern的映射规则

article/2025/10/2 18:32:48

原文出处:http://www.cnblogs.com/mailingfeng/archive/2012/04/05/2432687.html


Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单。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文件中,以下语法用于定义映射:

l. 以”/’开头和以”/*”结尾的是用来做路径映射的。

2. 以前缀”*.”开头的是用来做扩展映射的。

3. “/” 是用来定义default servlet映射的。

4. 剩下的都是用来定义详细映射的。比如: /aa/bb/cc.action

所以,为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断。


另外,关于url-pattern映射之后, request的servletContextPath , ServletPath , PathInfo 情况,可参照下面链接的文章

http://blog.csdn.net/cooljia/article/details/187882




=========================================================================================================

附上servlet3.1规范中的Mapping requests to servlet(12.1,12.2)

12.Mapping Requests to Servlets

The mapping techniques described in this chapter are required for Web containers

mapping client requests to servlets.

12.1 Use of URL Paths

Upon receipt of a client request, the Web container determines the Web application

to which to forward it. The Web applicatio n selected must have the longest context

path that matches the start of the request URL. The matched part of the URL is the

context path when mapping to servlets.

The Web container next must locate the servlet to process the request using the path

mapping procedure described below.

The path used for mapping to a servlet is  the request URL from the request object

minus the context path and the path parameters. The URL path mapping rules

below are used in order. The first successful match is used with no further matches

attempted:

1. The container will try to find an exact matc h of the path of the request to the path

of the servlet. A successful match selects the servlet.

2.  The container will recursively try to match the longest path-pre fix. This is done

by stepping down the path tree a directory at a time, using the  ’ / ’  character as a

path separator. The longest match  determines the servlet selected.

3.  If the last segment in the URL path contains an extension (e.g. .jsp), the servlet

container will try to match a servlet that  handles requests for the extension. An

extension is defined as the part of  the last segment after the last ’ . ’  character.

4.  If neither of the previous three rules result in a servlet match, the container will

attempt to serve content appropriate for the resource requested. If a "default"

servlet is defined for the application, it will be used. Many containers provide an

implicit default servlet for serving content.

The container must use case-sensitive string comparisons for matching.

12.2 Specification of Mappings

In the Web application deployment descriptor , the following syntax is used to define

mappings:

■ A string beginning with a  ‘ / ’  character and ending with a ‘ /*’  suffix is used for

path mapping.

■ A string beginning with a ‘ *.’  prefix is used as an extension mapping.

■ The empty string ("") is a special UR L pattern that exactly maps to the

application's context root, i.e., requests  of the form http://host:port/<context-root>/. In this case the path info is ’ / ’ and the servlet path and context path is

empty string (““).

■ A string containing only the ’ / ’  character indicates the "default" servlet of the

application. In this case the servlet path  is the request URI minus the context path and the path info is null.

■ All other strings are used for exact matches only.

If the effective  web.xml (after merging information from fragments and

annotations) contains any url-patterns that  are mapped to multiple servlets then the

deployment must fail.

12.2.1 Implicit Mappings

If the container has an internal JSP container, the  *.jsp extension is mapped to it,

allowing JSP pages to be executed on demand. This mapping is termed an implicit

mapping. If a *.jsp mapping is defined by the Web application, its mapping takes

precedence over the implicit mapping.

A servlet container is allowed to make ot her implicit mappings as long as explicit

mappings take precedence. For example, an implicit mapping of *.shtml could be

mapped to include functionality on the server.



http://chatgpt.dhexx.cn/article/KBgXg0Pq.shtml

相关文章

006_url-pattern配置

一. url-pattern配置 1. 在web.xml里面注册Servlet映射(servlet-mapping), 在Servlet里面我们需要配置url-pattern。url-pattern的作用就是在地址栏上的访问路径, 一定要以/打头。 2. 全路径匹配, 一级或者多级路径, 写法: /HWS或者/example/HWS。 请求路径: localhost:808…

Servlet学习(四):urlPattern配置与XML配置

1、urlPattern配置 Servlet类编写好后&#xff0c;要想被访问到&#xff0c;就需要配置其访问路径&#xff08;urlPattern&#xff09; 一个Servlet,可以配置多个urlPattern package com.itheima.web;import javax.servlet.ServletRequest; import javax.servlet.ServletRespo…

Makefile中变量的定义及赋值

1. 定义变量 Makefile 文件中定义变量的基本语法如下&#xff1a; 变量的名称值列表 Makefile 中的变量的使用其实非常的简单&#xff0c;因为它并没有像其它语言那样定义变量的时候需要使用数据类型。变量的名称可以由大小写字母、阿拉伯数字和下划线构成。等号左右的空白符…

makefile之.PHONY

1. 版本说明 首先说一下我用的make版本&#xff1a; GNU Make 4.2.1 2. 无PHONY无clean的情况&#xff08;无clean指当前目录下不存在clean文件&#xff09; 文件名&#xff1a;makefile (听说M必须大写&#xff0c;我用小写也可以执行成功) 文件内容&#xff1a; 且目录下…

Makefile文件:Makefile介绍

本文介绍Makefile的一些基本概念以及简单的用法。本文所用的编译器是Hightec tricore v4.9.1.0。 文章目录 1 Makefile的作用2 Makefile的规则3 一个简单的Makefile3.1 帮助文档中的例子3.2 例1&#xff1a;一个简单的Makefile 4 make是如何执行Makefile的4.1 默认目标4.2 例2…

Makefile入门详解

文章目录 一、Makefile简介二、makefile 原理1、当有依赖文件不存在2、当所有依赖文件存在 三、makefile基本规则1.makefile规则三要素2.基本规则3、makfile中的变量3.1普通变量3.2自动变量3.3例程 4、makefile中的伪目标 四、makefile其他常用的规则五、makefile中的函数5.1函…

makefile脚本

文章目录 1.makefile进行工程管理2.文档里面输入的内容3.如何运行这个makefile文件3.makefile 的变量4.其他 1.makefile进行工程管理 先创建一个名称为makefile或者Makefile的文档 2.文档里面输入的内容 输入相应内容的时候&#xff0c;要遵循相应的规则 规则&#xff1a;用…

makefile简介

1.make是一个应用程序 解析源程序之间的依赖关系 根据依赖关系自动维护编译工作 执行宿主操作系统中的各种命令 2.makefile是一个描述文件 定义一系列的规则来指定源文件编译的先后顺序 拥有特定的语法规则&#xff0c;支持函数定义和函数调用 能够直接集成操作系统中的各种命…

makefile变量

1.变量和不同的赋值方式 (1)makefile中支持程序设计语言中变量的概念 (2)makefile中的变量只代表文本数据(字符串) (3)makefile中的变量名规则 变量名可以包含字符&#xff0c;数字&#xff0c;下划线 不能包含“:”&#xff0c;"#"&#xff0c;"“或” " …

Makefile介绍

Makefile 是一种常用于编译的脚本语言&#xff0c;它可以更好更方便的管理你的项目的代码编译&#xff0c;节约编译时间&#xff08;没改动的文件不编译&#xff09;。 注意 Makefile 文件命令必须是 Makefile 或者 makefile&#xff0c;并使用 make 命令编译。 1. 1个…

如何编写一个Makefile文件(手把手的教你)

如果有帮助&#xff0c;希望点赞支持&#xff0c;我会更有创作的动力哦 目录 一、概念理解&#xff08;彩蛋藏在某个地方&#xff09;1.1 什么是Makefile&#xff1f;1.2 为何使用Makefile&#xff1f; 二、实战代码演示与讲解2.1 没有makefile的项目是怎么创建运行的2.1.1 创建…

Makefile入门(超详细一文读懂)

1、Makefile编译过程 Makefile文件中的命令有一定规范&#xff0c;一旦该文件编写好以后在Linux命令行中执行一条make命令即可自动编译整个工程。不同厂家的make可能会稍有不同&#xff0c;并且语法上也有区别&#xff0c;不过基本思想都差不多&#xff0c;主要还是落在目标依赖…

刘玉真先生语录

或问&#xff1a;“古今之法门多矣&#xff0c;何以此教独名‘净明忠孝’ ? ” 先生曰&#xff1a;“别无他说&#xff0c;‘净明’只是正心诚意&#xff0c;‘忠孝’只是扶植纲常。但世人习闻此语&#xff0c;多是忽略过去&#xff0c;此间却务真践实履。” 先生曰&#xff1…

俞敏洪用20年的经验笑谈人生:不要在穷的时候假装崇高

转载于: https://www.huxiu.com/article/174774/1.html 虎嗅注&#xff1a;本文是新东方创始人、洪泰基金联合创始人俞敏洪&#xff0c;于12月1日在麻省理工学院跟学生做的一个主题演讲&#xff0c;言辞幽默&#xff0c;充满智慧&#xff0c;有太多人生的道理。本文由微信公众号…

每日言论:『模仿他人是人生陷阱』

公众号关注 「奇妙的 Linux 世界」 设为「星标」&#xff0c;每天带你玩转 Linux &#xff01; 最近&#xff0c;我们建立了一个技术交流微信群。目前群里已加入了不少行业内的大神&#xff0c;有兴趣的同学可以加入和我们一起交流技术&#xff0c;在 「奇妙的 Linux 世界」 公…

38岁,外企技术经理,失业:职场遇到瓶颈,你可能掉进了“能力陷阱”!

作者| Mr.K 编辑| Emma 来源| 技术领导力(ID&#xff1a;jishulingdaoli) 一位读者小R&#xff0c;给我讲述了他的职场经历。 小R&#xff0c;2008年通信专业硕士毕业。先去了华为&#xff0c;做了1年觉得有点苦&#xff0c;就去学了1年英语&#xff0c;后来跳槽到摩托罗拉。…

复旦大学教师 于娟博士《为啥是我得癌症?》

复旦女教师于娟已经去世半年多了&#xff0c;但这篇《为啥是我得癌症&#xff1f;》值得每个人认真阅读。 于娟&#xff0c;女&#xff0c;32岁&#xff0c;祖籍山东济宁&#xff0c;海归&#xff0c;博士&#xff0c;复旦大学优秀青年教师&#xff0c;一个两岁孩子的母亲&a…

《思考致富》不应该指望不经历“暂时的失败”便能发财

目录 作者简介 经典摘录 机遇有个狡猾的习惯&#xff0c;喜欢从后门悄悄溜进来&#xff0c;往往还喜欢以灾难或暂时失败的方式乔装露面 离金矿仅有三英尺远 欲望&#xff1a;成就一切的起点&#xff08;通往致富之路的第一步&#xff09; 信念&#xff1a;在脑海里目睹并坚…

复旦女博士于娟:为啥是我得癌症?

复旦女博士于娟&#xff1a;为啥是我得癌症&#xff1f;【请所有的朋友看看此文】 复旦女教师于娟已经去世半年多了&#xff0c;但这篇《为啥是我得癌症&#xff1f;》值得每个人认真阅读。不要再瞎吃八吃、暴饮暴食、嗜荤如命&#xff0c;不要再拼命工作、天天熬夜&#xff0…

[转载]复旦女博士于娟——为啥是我得癌症? (转)

[转载]复旦女博士于娟——为啥是我得癌症? (转) (2012-07-30 13:47:00) 转载▼ 标签&#xff1a; 转载 黄帝内经之类。就此引用一段话&#xff1a; 下午5--7点酉时 肾经当令 晚上7--9点戌时 心包经当令 9-11点亥时 三焦经当令 11-1点子时 胆经当令 凌晨1--3点丑时 肝经当令 3…