search
HomeWeb Front-endJS TutorialThe url-pattern setting method and mapping rules of servlet and filter



<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei color:rgb line-height:1.5></span><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial," microsoft yahei line-height:19px word-break:break-all background-color:rgb><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单。servlet和filter中的url-pattern有一些文章在里面的,总结了一些东西,以免遇到问题又要浪费时间。 <br>   </span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial," microsoft yahei line-height:19px word-break:break-all background-color:rgb><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">一,servlet容器对url的匹配过程: <br><br>当 一个请求发送到servlet容器的时候,容器先会将请求的url减去当前应用上下文的路径作为servlet的映射url,比如我访问的是 http://localhost/test/aaa.html,我的应用上下文是test,容器会将http://localhost/test去掉, 剩下的/aaa.html部分拿来做servlet的映射匹配。这个映射匹配过程是有顺序的,而且当有一个servlet匹配成功以后,就不会去理会剩下 的servlet了(filter不同,后文会提到)。其匹配规则和顺序如下: <br><br><span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>1.     精确路径匹配。</span>例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的 servlet了。 <br><br><span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>2.     最长路径匹配</span>。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此 时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。 <br><br><span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>3.     扩展匹配</span>,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA的url-pattern:*.action <br><br>4.     如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>default servlet</span>,则容器会将请求丢给default servlet(什么是default servlet?后面会讲)。 <br><br>     根据这个规则表,就能很清楚的知道servlet的匹配过程,所以定义servlet的时候也要考虑url-pattern的写法,以免出错。 <br><br>      对于filter,不会像servlet那样只匹配一个servlet,因为filter的集合是一个链,所以只会有处理的顺序不同,而不会出现只选择一 个filter。Filter的处理顺序和filter-mapping在web.xml中定义的顺序相同。 <br>   </span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial," microsoft yahei line-height:19px word-break:break-all background-color:rgb><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">二,url-pattern详解 <br><br>         在web.xml文件中,以下语法用于定义映射: <br><br>l. 以<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>”/’开头</span>和<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>以”/*”结尾</span>的是用来做<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>路径映射</span>的。 <br><br>2. 以<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>前缀”*.”开头</span>的是用来做<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>扩展映射</span>的。 <br><br>3. <span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>“/”</span> 是用来定义<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>default servlet映射</span>的。 <br><br>4. 剩下的都是用来定义<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>详细映射</span>的。比如: /aa/bb/cc.action <br><br>所以,为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为<span style="margin:0px; padding:0px; font-family:Arial," microsoft yahei>这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断</span>。</span></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:Arial," microsoft yahei line-height:19px word-break:break-all background-color:rgb><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">另外,关于url-pattern映射之后, request的servletContextPath , ServletPath , PathInfo 情况,可参照下面链接的文章</span></p> 
 1 servlet与filter的url-pattern设置方式: 
 2  
 3 1、精确匹配: 
 4 /directory/file1.jsp 
 5 /directory/file2.jsp 
 6 /directory/file3.jsp 
 7  
 8 2、目录匹配: 
 9 /directory/*
 10 
 11 3、扩展匹配:
 12 *.jsp
 13 
 14 注意:下面的不支持:
 15 /direcotry/*.jsp
 16 
 17 /和/*之间的区别:
 18 <url-pattern>/</url-pattern>: 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
 19 <url-pattern>/*</url-pattern>:会匹配所有url:路径型的和后缀型的url(包括/login , *.jsp , *.js 和 *.html 等)
 20 <url-pattern>/</url-pattern>: 甚至会造成The requested resource () is not available.

The above is the detailed content of The url-pattern setting method and mapping rules of servlet and filter. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools