问题
请求到达server以后,server是怎样处理URI请求资源路径的,在与web.xml文件里的映射进行比对时的原则是什么。
方案
针对精确匹配、通配符匹配、后缀匹配三种模式改动web.xml文件里的配置,查看不同比对原则的訪问结果
步骤
step1:新建URIServlet.java 文件
代码例如以下:
package web;
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 URIServlet extends HttpServlet{
protected void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
System.out.println("这里是URIServlet的service()方法");
}
}
step2:改动web.xml文件
例如以下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>uri1</servlet-name>
<servlet-class>web.URIServlet</servlet-class>
</servlet>
<!-- URI匹配,每次仅仅能开放一个servlet-mapping -->
<!--
<servlet-mapping>
<servlet-name>uri1</servlet-name>
<url-pattern>/abc/abc.html</url-pattern>
</servlet-mapping>
-->
<servlet-mapping>
<servlet-name>uri1</servlet-name>
<url-pattern>/abc/*</url-pattern>
</servlet-mapping>
<!--
<servlet-mapping>
<servlet-name>uri1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
-->
</web-app>
step3:新建uri.html文件、測试
例如以下:
<!DOCTYPE html>
<html>
<head>
<title>uri.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body style="font-size:24">
<h3>请求资源路径的 处理</h3>
<ul>
<li style="margin:40px">精确匹配
<ul>
<li>url-pattern:/abc/abc.html
<li><a href="abc.html">abc.html</a>
<li><a href="abc/abc.html">abc/abc.html</a>
</ul>
<li style="margin:40px">通配符匹配
<ul>
<li>url-pattern:/abc/*
<li><a href="abc.html">abc.html</a>
<li><a href="abc/abc.html">abc/abc.html</a>
</ul>
<li style="margin:40px">精确匹配 //注意li/ul的使用,效果例如以下图
<ul>
<li>url-pattern:*.do
<li><a href="abc.do">abc.do</a>
<li><a href="abc/abc.do">abc/abc.do</a>
</ul>
</ul>
</body>
</html>
==============================
測试界面例如以下所看到的:
測试成功时:在Console会输出:
这里是URIServlet的service()方法
(出如今Server startup in 36041 ms的以下)
刚開始測试时,出现一个错误:
严重: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/URIServletTest]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1898)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name uril
原因:web.xml文件里同一时候包括了多个
解决方法:每次仅仅开启一个,不用的凝视掉