Home Web Front-end JS Tutorial The url-pattern setting method and mapping rules of servlet and filter

The url-pattern setting method and mapping rules of servlet and filter

Sep 20, 2017 am 10:03 AM
filter servlet



<span style='margin:0px; padding:0px; font-family:Arial,"Microsoft YaHei"; color:rgb(0,128,128); line-height:1.5!important'></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(254,254,242)'><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(254,254,242)'><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(254,254,242)'><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(254,254,242)'><span style="margin:0px; padding:0px; font-family:verdana,sans-serif; line-height:21px">另外,关于url-pattern映射之后, request的servletContextPath , ServletPath , PathInfo 情况,可参照下面链接的文章</span></p> 
Copy after login
 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.
Copy after login

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
The servlet life cycle is divided into several stages The servlet life cycle is divided into several stages Feb 23, 2023 pm 01:46 PM

The Servlet life cycle refers to the entire process from creation to destruction of a servlet, which can be divided into three stages: 1. Initialization stage, calling the init() method to initialize the Servlet; 2. Running stage (processing requests), the container will Request to create a ServletRequest object representing an HTTP request and a ServletResponse object representing an HTTP response, and then pass them as parameters to the service() method of the Servlet; 3. Destruction phase.

What is a servlet What is a servlet Jan 28, 2023 am 09:51 AM

The full name of Servlet is "Java Servlet", which means small service program or service connector in Chinese. It is a program running on a Web server or application server. It serves as a request from a Web browser or other HTTP client and a database on the HTTP server or The middle layer between applications. Servlet has the characteristics of being independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content.

How to solve the '[Vue warn]: Failed to resolve filter' error How to solve the '[Vue warn]: Failed to resolve filter' error Aug 19, 2023 pm 03:33 PM

Methods to solve the "[Vuewarn]:Failedtoresolvefilter" error During the development process using Vue, we sometimes encounter an error message: "[Vuewarn]:Failedtoresolvefilter". This error message usually occurs when we use an undefined filter in the template. This article explains how to resolve this error and gives corresponding code examples. When we are in Vue

How does Java Servlet implement distributed session management? How does Java Servlet implement distributed session management? Apr 16, 2024 pm 02:48 PM

There are two ways to implement distributed session management in JavaServlet: 1. Session replication: Copy session data to each server. 2. Session distribution: Use a centralized storage service to store session data and access it from multiple servers. The specific implementation methods are: session replication configures true in the web. session data.

What are the application scenarios of Java Servlet? What are the application scenarios of Java Servlet? Apr 17, 2024 am 08:21 AM

JavaServlet can be used for: 1. Dynamic content generation; 2. Data access and processing; 3. Form processing; 4. File upload; 5. Session management; 6. Filter. Example: Create a FormSubmitServlet to handle form submission, taking name and email as parameters, and redirecting to success.jsp.

Java technology stack for web development: Understand Java EE, Servlet, JSP, Spring and other technologies commonly used in web development Java technology stack for web development: Understand Java EE, Servlet, JSP, Spring and other technologies commonly used in web development Dec 26, 2023 pm 02:29 PM

JavaWeb development technology stack: Master JavaEE, Servlet, JSP, Spring and other technologies used for Web development. With the rapid development of the Internet, in today's software development field, the development of Web applications has become a very important technical requirement. As a widely used programming language, Java also plays an important role in the field of Web development. The JavaWeb development technology stack involves multiple technologies, such as JavaEE, Servlet, JSP, Spr

Servlet Container Revealed: A Deeper Understanding of the Servlet Runtime Environment Servlet Container Revealed: A Deeper Understanding of the Servlet Runtime Environment Feb 19, 2024 pm 01:00 PM

The Servlet container is an application that provides the Servlet running environment. It is responsible for managing the life cycle of the Servlet and providing necessary WEB services, such as security, transactions, etc. There are many types of Servlet containers, the most common of which are Tomcat and Jetty. The main functions of the Servlet container are life cycle management: The Servlet container is responsible for managing the life cycle of the Servlet, including startup, initialization, service and destruction. Web services: The Servlet container provides web services, such as security, transactions, etc. Resource management: Servlet container manages resources, such as Servlet, jsP, html pages, etc. Class loading: The Servlet container is responsible for adding

Java Errors: Servlet Errors, How to Fix and Avoid Java Errors: Servlet Errors, How to Fix and Avoid Jun 25, 2023 pm 06:34 PM

Servlet is a very commonly used technology in Java Web application development. However, some Servlet errors will inevitably occur during the development process. How to solve and avoid Servlet errors has become a top issue for many Java developers. This article will introduce some common Servlet errors and their solutions based on personal experience and related information. ClassNotFoundException When we try to load a class, if the class does not exist or cannot be accessed by the system,

See all articles