访问servlet的路径问题

1、url-pattern的三种配置

  在web.xml配置文件中配置有关Servlet的时候,<url-pattern>标签是用于配置当前Servlet拦截的路径,也就是说,客户端浏览器访问<url-pattern>标签配置的路径才能访问对应Servlet内容。html

关于拦截路径的配置方式其实有三种方式:java

  1. 彻底路径匹配:是以“/”开始,路径中间不能包含通配符“*”,例如:/firstServclet,表示访问路径为http://localhost:8080/08_servlet/firstServlet。
  2. 目录匹配:是以“/”开始,以“/*”结尾的,例如:/firstServlet/*,表示访问路径为http://localhost:8080/08_servlet/firstServlet路径下任意内容。
  3. 扩展名匹配:是以“*”开始,不能以“/”开始,以“.xxx”结尾,例如:*.do,表示访问路径为全部扩展名为“.do”的路径。

值得注意的问题:web

  1. 在一个<servlet-mapping>标签中,能够配置多个<url-pattern>标签。也就是说,一个Servlet能够拦截多个不一样路径的访问。
  2. 上述三种配置路径方式具备优先级:彻底路径匹配 -> 目录匹配 -> 扩展名匹配。

下面经过一些测试,来看看路径配置的三种方式:apache

以下有一些映射关系:浏览器

  1. Servlet1 映射到 /abc/*
  2. Servlet2 映射到 /*
  3. Servlet3 映射到 /abc
  4. Servlet4 映射到 *.do

问题:服务器

  • 当请求URL为“/abc/a.html”,“/abc/*”和“/*”都匹配,哪一个servlet响应?Servlet1
  • 当请求URL为“/abc”时,“/abc/*”和“/abc”都匹配,哪一个servlet响应?Servlet3
  • 当请求URL为“/abc/a.do”时,“/abc/*”和“*.do”都匹配,哪一个servlet响应?Servlet1
  • 当请求URL为“/a.do”时,“/*”和“*.do”都匹配,哪一个servlet响应?Servlet2
  • 当请求URL为“/xxx/yyy/a.do”时,“/*”和“*.do”都匹配,哪一个servlet响应?Servlet2

  若是客户端浏览器请求的路径是错误时,页面会显示404错误内容。这是由于全部发布到Tomcat服务器的Web应用程序的web.xml文件都继承了Tomcat服务器安装目录中conf目录中的web.xml文件。当访问路径是错误的,或者对应Servlet没有配置,实际上会执行Tomcat服务器中的web.xml的相关配置,具体内容以下:app

 1 <servlet>
 2     <servlet-name>default</servlet-name>
 3 
 4 <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
 5     <init-param>
 6         <param-name>debug</param-name>
 7         <param-value>0</param-value>
 8     </init-param>
 9     <init-param>
10         <param-name>listings</param-name>
11         <param-value>true</param-value>
12     </init-param>
13     <load-on-startup>1</load-on-startup>
14 </servlet>
15 <servlet-mapping>
16     <servlet-name>default</servlet-name>
17     <url-pattern>/</url-pattern>
18 </servlet-mapping>

 

1.2. 相对路径与绝对路径

以前咱们开发的Servlet,在客户端浏览器中都是直接在地址栏中输入路径来访问的。若是建立一个页面来访问Servlet应该怎么样呢?下面咱们来看一看:jsp

  • Web工程中的WebRoot目录下,建立一个新目录名为“html”,而后在该目录下建立一个新的HTML页面。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">测试

<html>ui

  <head>

    <title>01.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">

  </head>

  <body>

    <h1>相对路径访问Servlet</h1><br>

<a href="">相对路径访问Servlet</a>

<h1>绝对路径访问Servlet</h1><br>

<a href="">绝对路径访问Servlet</a>

  </body>

</html>

  • Web工程中的WebRoot目录下,建立一个新的HTML页面。

 

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3   <head>
 4 <title>02.html</title>
 5     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 6     <meta http-equiv="description" content="this is my page">
 7     <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 8   </head>
 9   <body>
10     <h1>相对路径访问Servlet</h1><br>
11     <a href="">相对路径访问Servlet</a>
12     <h1>绝对路径访问Servlet</h1><br>
13     <a href="">绝对路径访问Servlet</a>
14   </body>
15 </html>
  • Web工程中建立一个Servlet。

 

public class PathServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("成功访问到Servlet");
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

 

  • 配置Web工程的web.xml文件。

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    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_2_5.xsd">
  <servlet>
    <servlet-name>PathServlet</servlet-name>
    <servlet-class>app.java.servlet.PathServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>PathServlet</servlet-name>
    <url-pattern>/pathServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  • 将当前Web工程发布到Tomcat服务器,并启动Tomcat服务器。
  • 打开浏览器,分别访问01.html、02.html和PathServlet。

 访问01.html的路径:http://localhost:8080/08_servlet/html/01.html

 访问02.html的路径:http://localhost:8080/08_servlet/02.html

 访问PathServlet的路径:http://localhost:8080/08_servlet/pathServlet

  • 根据上述的访问路径,能够知道在01.html和02.html页面中,经过绝对路径访问PathServlet是相同的。
  • 01.html和02.html页面中,经过相对路径访问PathServlet是不一样的。

01.html页面中利用相对路径访问PathServlet应该是../pathServlet。缘由是pathServlet是在01.html页面的父级目录中。

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>01.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">
  </head>
  <body>
    <h1>相对路径访问Servlet</h1><br>
    <a href="../pathServlet">相对路径访问Servlet</a>
    <h1>绝对路径访问Servlet</h1><br><a href="http://localhost:8080/08_servlet/pathServlet">绝对路径访问Servlet</a>
</body></html>

² 01.html页面中利用相对路径访问PathServlet应该是./pathServlet或直接访问拦截名称pathServlet。缘由是pathServlet与02.html页面处在同一级别的目录中。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>02.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">
  </head>
  <body>
    <h1>相对路径访问Servlet</h1><br>
    <a href="pathServlet">相对路径访问Servlet</a>
    <h1>绝对路径访问Servlet</h1><br>
    <h1>绝对路径访问Servlet</h1><br>
    <a href="http://localhost:8080/08_servlet/pathServlet">  绝对路径访问Servlet</a>
  </body>
</html>

什么是绝对路径与相对路径:

  • 绝对路径:就是不管当前资源在什么位置,都能经过当前资源访问到目标资源。
  • 相对路径:就是判断当前资源与目标资源的相对位置,找出相对当前资源能够访问到目标资源的路径。
相关文章
相关标签/搜索