本篇来做一个Servlet映射的练习,通过几个练习,了解Servlet是可以根据一个Servlet对象,映射出不同的访问URI。而且映射可以写类似正则的表达式,映射也存在优先级顺序问题。
1.准备一个ServletDemo3类
package com.anthony.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*****ServletDemo3********");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
添加映射关系在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_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>servletDemo3</servlet-name>
<servlet-class>com.anthony.servlet.ServletDemo3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
部署,浏览器打开Url测试,我们是可以通过http://localhost:8080/Servlet01/demo3访问。
然后我这里再copy一组映射
<?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_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>servletDemo3</servlet-name>
<servlet-class>com.anthony.servlet.ServletDemo3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo123</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo123/abc</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
再次部署到tomcat服务器上,我们发现还是可以通过以下url进行访问
http://localhost:8080/Servlet01/demo123
http://localhost:8080/Servlet01/demo123/abc
上面三个,我们只定义了一个name为servletDemo3的servlet对象,然后写了三种映射,而且三种映射访问都支持。所以多个映射路径可以指向同一个servlet对象
2.Servlet映射采用通配符
上面我们写了三个mapping,其实我们如果需要多个映射,可以采用通配符来解决。
语法、
1)* 表示任意字符
2)*.xx , 匹配以 *.xx的url都可以访问
3)/action/*, 以action开头都可以匹配
这里来模仿一个/abc/*的 url-parentr
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/abc/*</url-pattern>
</servlet-mapping>
重新发布,测试一下地址
http://localhost:8080/Servlet01/abc/
http://localhost:8080/Servlet01/abc/1
http://localhost:8080/Servlet01/abc/abc
http://localhost:8080/Servlet01/abc/1.php
3.映射优先级试题
有时候笔试中关于servlet有一道试题考察映射优先级的顺序问题。
优先级规则:完全匹配 > /开头方式匹配 > 扩展名方式匹配
对于如下的一些映射关系:
servlet1 映射到 /abc/*
servlet2 映射到 /*
servlet3 映射到 /abc
servlet4 映射到 *.do
问题:
当请求URL为 “/abc/a.html", 哪个servlet将响应。
A:1
当请求URL为 “/abc", 哪个servlet将响应。
A:3
当请求URL为 “/abc/a.do", 哪个servlet将响应。
A:1
当请求URL为 “/a.do", 哪个servlet将响应。
A:2
当请求URL为 “/xxx/yyy/a.do", 哪个servlet将响应。
A:2
分析
第一题,请求URL为“/abc/a.html” , 匹配 Servlet1的 “/abc/*”, 也匹配Servlet2的 "/*", 根据规则 /abc这块属于完全匹配,所以Servlet1会响应。第二题 同时匹配2 和3,最终Servlet 3会响应,这个属于完全匹配。第三题 同时匹配1和4,但是/开头优先级更高,所以Servlet1会响应。后面四五题自己根据规则去分析下,上面已经给出答案。