访问servlet的路径问题

一、url-pattern的三种配置

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

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

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

值得注意的问题:

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

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

如下有一些映射关系:

  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的相关配置,具体内容如下:

 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应该怎么样呢?下面我们来看一看:

  • Web工程中的WebRoot目录下,创建一个新目录名为“html”,然后在该目录下创建一个新的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="">相对路径访问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>

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

  • 绝对路径:就是无论当前资源在什么位置,都能通过当前资源访问到目标资源。
  • 相对路径:就是判断当前资源与目标资源的相对位置,找出相对当前资源可以访问到目标资源的路径。

转载于:https://www.cnblogs.com/god-y-l/p/6847110.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值