ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

11.Servlet虚拟路径匹配规则

2022-07-31 13:35:47  阅读:151  来源: 互联网

标签:11 匹配 http servletDemo 虚拟 8080 Servlet localhost


当 Servlet 容器接收到请求后,容器会将请求的 URL 减去当前应用的上下文路径,使用剩余的字符串作为映射 URL 与 Servelt 虚拟路径进行匹配,匹配成功后将请求交给相应的 Servlet 进行处理。

以 servletDemo 为例,若 URL 为“http://localhost:8080/servletDemo/myServlet”,其应用上下文是 servletDemo,容器会将“http://localhost:8080/servletDemo”去掉,使用剩余的“/myServlet”与 Servlet 虚拟路径进行匹配。

匹配规则

Servlet 虚拟路径匹配规则有以下 4 种:

  1. 完全路径匹配
  2. 目录匹配
  3. 扩展名匹配
  4. 缺省匹配(默认匹配)


下面我们以 servletDemo 为例,分别介绍 4 种规则。

匹配规则 使用规则 虚拟路径 可访问的URL
完全路径匹配
(精确匹配)
/开始,不能包含通配符
必须完全匹配
/myServlet
/user/myServlet
/product/index.action
http://localhost:8080/servletDemo/myServlet
http://localhost:8080/servletDemo/user/myServlet
http://localhost:8080/servletDemo/product/index.action
目录匹配 /字符开头,并以/*结尾的字符串。
用于路径匹配
/user/*
/*
http://localhost:8080/servletDemo/user/aaa
http://localhost:8080/servletDemo/aa
扩展名匹配 以通配符*.开头的字符串。
用于扩展名匹配
*.do
*.action
*.jsp
http://localhost:8080/servletDemo/user.do
http://localhost:8080/servletDemo/myServlet.action
http://localhost:8080/servletDemo/bb.jsp
缺省匹配(默认匹配) 映射路径为/,表示这个 Servlet 为当前应用的缺省 Servlet 或默认 Servlet,默认处理无法匹配到虚拟路径的请求。 / 可以匹配任意请求 URL


注意:目录匹配和扩展名匹配无法混合使用,即/rest/*.do这种写法是不正确的。

匹配优先级

Servlet 虚拟路径的匹配优先级顺序为:全路径匹配(精确匹配)> 目录匹配 > 扩展名匹配 > 缺省匹配(默认匹配)。

Servlet 容器会从优先级高的虚拟路径开始匹配,匹配成功后就会立刻将请求交给相应的 Servlet 进行处理,不会再关注其他虚拟路径是否匹配成功。

示例 1

下面我们通过一个实例加深对 Servlet 虚拟路径匹配的理解。

在 servletDemo 的 net.biancheng.www 包下,创建名称为 VirtualPathServlet 的 Servlet 类,代码如下。

package net.biancheng.www;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class VirtualPathServlet extends HttpServlet {


private static final long serialVersionUID = 1L;


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
// 向页面输出
writer.write("本次访问的Servlet是:" + this.getServletName());
writer.close();
}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}


}


web.xml 的配置如下。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" metadata-complete="false" version="4.0">


<servlet>
<servlet-name>MyServlet1</servlet-name>
<servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MyServlet2</servlet-name>
<servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MyServlet3</servlet-name>
<servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MyServlet4</servlet-name>
<servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MyServlet5</servlet-name>
<servlet-class>net.biancheng.www.VirtualPathServlet</servlet-class>
</servlet>


<!-- 完全路径匹配 -->
<servlet-mapping>
<servlet-name>MyServlet1</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!-- 目录匹配 -->
<servlet-mapping>
<servlet-name>MyServlet2</servlet-name>
<url-pattern>/abc/my/*</url-pattern>
</servlet-mapping>
<!-- 目录匹配 -->
<servlet-mapping>
<servlet-name>MyServlet3</servlet-name>
<url-pattern>/abc/*</url-pattern>
</servlet-mapping>
<!-- 扩展名匹配 -->
<servlet-mapping>
<servlet-name>MyServlet4</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--缺省匹配 -->
<servlet-mapping>
<servlet-name>MyServlet5</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>


启动 Tomcat 服务器,在地址栏中输入“http://localhost:8080/servletDemo/hello”,结果如下图。

/hello


在地址栏中输入“http://localhost:8080/servletDemo/abc/my/login”,结果如下图。


在地址栏中输入“http://localhost:8080/servletDemo/abc/index.do”,结果如下图。


在地址栏中输入“http://localhost:8080/servletDemo/hello/index.do”,结果如下图。


在地址栏中输入“http://localhost:8080/servletDemo/hello/pay.html”,结果如下图。

/abc/index.do

Tomcat 中的缺省 Servlet

在 Tomcat 安装目录的 \conf\web.xml 文件中,注册了一个名称为 org.apache.catalina.servlets.DefaultServlet 的 Servlet,并将它设置为缺省 Servlet。

<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


Tomcat 服务器中的 Web 应用没有缺省 Servlet 时,会将 DefaultServlet 作为其缺省 Servlet。当客户端访问 Tomcat 服务器中某个静态 HTML 文件或者图片时,DefaultServlet 会判断该 HTML 或图片是否存在,若存在,则将数据以流的形式返回客户端,否则会报告 404 错误。

示例 2

将 servletDemo 应用的缺省匹配删除,并在 servletDemo 应用里添加一张图片 a.jpg。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" metadata-complete="false" version="4.0">


<!--使用tomcat的缺省Servlet访问静态资源或图片 -->
<!--需要先将项目内的缺省Servlet删除 -->
<!--缺省匹配 -->
<!--<servlet-mapping>
<servlet-name>MyServlet5</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>-->
</web-app>


启动 Tomcat,在地址栏输入“http://localhost:8080/servletDemo/a.jpg”,结果如下图。

缺省模式

标签:11,匹配,http,servletDemo,虚拟,8080,Servlet,localhost
来源: https://www.cnblogs.com/55zjc/p/16536955.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有