在JavaWeb中需要写的路径大概分为四大类:

1)客户端路径:超链接、表单、重定向

2)服务端路径:转发、包含

3)获取资源路径:servletContext获取资源、ClassLoader获取资源、Class获取资源

4) 路径:构建Web项目的目录: url-pattern

一、客户端路径

客户端路径是指运行在浏览器上的路径。
  比如:表单、超链接、js(location.href)、Ajax(url)、CSS和JS的引入以及重定向等。路径分为绝对路径和相对路径,
  
  相对路径又分为相对主机的路径和相对于当前请求的路径。
1.1、超链接 
  超链接有三种书写路径的方式
    1)绝对路径
    2)以"/“开头的相对路径
    3)不以”/"开头的相对路径

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>页面A</title>  
</head>  
<body>  
    <!--   
        超链接有三种书写路径的方式  
            1,绝对地址    
            2,以"/"开头的相对地址  
            3,不以"/"开头的相对地址   
    -->  
    <!-- 1.绝对地址   -->  
        <!-- 完整的URL -->  
    <a href="http://localhost:8080/javaee/jsp/b.jsp">这是绝对地址超链接</a><br/>  
      
    <!-- 2.以"/"开头的相对地址    -->  
        <!-- /代表了整个web项目,即:http://localhost:8080/ -->  
    <a href="/javaee/jsp/b.jsp">这是以"/"开头的相对地址超链接</a><br/>  
      
    <!-- 3.不以"/"开头的相对地址   -->  
        <!--   
            不以/开头,则相对于当前资源的路径  
            当前资源的路径为:http://localhost:8080/javaee/jsp/  
            而b.jsp也在此路径下  
            所以直接书写b.jsp  
         -->  
    <a href="b.jsp">这是不以"/"开头的相对地址超链接</a><br/>  
</body>  
</html>

分析:   
  1)绝对路径(以协议开头的路径):最终请求路径为:http://localhost:8080/javaee/jsp/b.jsp   2)相对路径:
    2.1) 相对于主机的路径(以“/”开头):相对于当前主机(可以简单理解为ip地址,如果想深入了解请研究tomcat的Server.xml文件。
    这里是localhost)的路径,请求的最终路径为:http://localhost:8080/javaee/jsp/b.jsp   2.2)相对于请求的路径(不以“/”开头):相对于当前请求(浏览器的请求)的路径。

二、服务端路径

服务端路径是指在服务器上面运行的请求,比如请求转发(常用)、请求包含等。服务端的路径有两种:相对于当前应用的路径和相对于当前请求的路径。

2.1、请求转发

请求转发有两种书写路径的方式
    2)以"/“开头的相对路径
    3)不以”/"开头的相对路径
  注意:服务器端的路径不能是绝对路径,只能是相对路径,也分为以/开头和不以/开头两种

import java.io.IOException;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
/* 
 *      1.以"/"开头的相对路径 
 *      2.不以"/"开头的相对路径 
 */  
public class DispatcherServlet extends HttpServlet {  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        /* 
         * 1.以"/"开头的相对路径 
         *      此时,/代表当前web项目,即:http://localhost:8080/javaee 
         */  
//      request.getRequestDispatcher("/jsp/b.jsp").forward(request, response);  
        /* 
         * 2.不以"/"开头的相对路径 
         *      相对于当前资源的相对路径 
         *  此时,当前资源的路径为:http://localhost:8080/javaee/DispatcherServlet 
         *  所以要转发去的资源的路径以:http://localhost:8080/javaee开头 
         */  
        request.getRequestDispatcher("jsp/b.jsp").forward(request, response);  
    }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        doGet(request, response);  
    }  
  
}

注意:所有Web层框架的底层使用的都是Web的基础Filter(Struts)或Servlet(SpringMVC),请求都是request,响应都是response,所以各个Web层框架的转发或重定向底层都是利用request和response进行的。

三、资源获取路径

**获取资源的路径主要有3种,分别是ServletContext、Class和ClassLoader。
**
其中ServletContext是WEB阶段的,Tomcat提供的一种获取资源的方式;

Class和ClassLoader获取资源主要是JavaAPI提供的一种获取流的方式,由于这是JDK提供的,所以不仅局限于Web,在普通Java类中也可以使用,主要用于获取src目录及其子目录下的文件流。

3.1、ServletContext获取资源

这里ServletContext获取资源的路径是相对系统的绝对路径(在Windows中是带盘符的,可以用来获取上传或下载文件的具体路径)。

基本语法:

servletContext.getRealPath("路径");

参数中的路径必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径,建议以"/"开头(这样可以尽量统一)。

另外获取ServletContext的方法如下:
    1)使用request获取: request.getSession().getServletContext();
    2)在Servlet中获取:this.getServletContext();
    3)使用FilterConfig对象获取(在Filter中使用):config.getServletContext();

ServletContext获取资源必须是相对路径,不能是绝对路径,但不管是以/开头,还是不以/开头, 都是相对于当前资源的相对路径 。

import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

public class ServletContextServlet extends HttpServlet {  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        String path1 = this.getServletContext().getRealPath("/a.properties");  
        String path2 = this.getServletContext().getRealPath("a.properties");  
        System.out.println(path1);  
        System.out.println(path2);  
        //输出的地址一样  
    }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        doGet(request, response);  
    }  
  
}

3.2、ClassLoader获取资源

和Class获取资源类似。只是不同的写法而已,也是用于获取文件流的。

用法:

classLoader.getResourceAsStream("路径")。

参数中的路径可以以“/”开头,也可以不以“/”开头(建议)。但带不带“/”的都表示相对于当前类的路径。

ClassLoader类加载器不能通过绝对地址来加载资源,只能通过相对地址来加载资源 但相对地址不管前面加不加/都是相当于类路径的相对地址

public class ClassLoaderServlet extends HttpServlet {  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        /* 
         * 加了/,其地址是相对于类路径的相对地址 
         */  
//      InputStream in = this.getClass().getClassLoader().getResourceAsStream("/cn/ccnu/classloaderpath/c.properties");  
//      Properties prop = new Properties();  
//      prop.load(in);  
//      System.out.println(prop.getProperty("url"));  
          
        /* 
         * 不加/,其地址是相对于类路径的相对地址 
         */  
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/classloaderpath/c.properties");  
        Properties prop = new Properties();  
        prop.load(in);  
        System.out.println(prop.getProperty("url"));  
        /* 
         * 总结:不能使用绝对地址,而只能只用相对地址 
         * 且不管加不加/的相对地址,都是相对于类路径的相对地址 
         *  
         */  
    }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        doGet(request, response);  
    }  
  
}

3.3、Class获取资源

Class获取资源主要是用作自己写的配置文件,用来读取内容。

用法:

clazz.getResourceAsStream("路径")。

参数中的路径可以以“/”开头,也可以不以“/”开头。其中带“/”的表示相对于当前类的路径,不以“/”开头表示相对于当前class所在目录的路径。

Class读取资源不能是绝对路径,只能是相对路径,又分为以/开头或者是不以/开头 。

public class ClassServlet extends HttpServlet {  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        /* 
         * 1.以/开头的相对路径 
         *      此时的/代表类路径,即:/javaee/WEB-INF/classes 
         */  
//      InputStream in = ClassServlet.class.getResourceAsStream("/cn/ccnu/classpath/b.properties");  
//      Properties porp = new Properties();  
//      porp.load(in);  
//      System.out.println(porp.getProperty("url"));  
        /* 
         * 2.不以/开头的相对路径 
         *      此时相对的是:类ClassServlet.class的路径,即:\javaee\WEB-INF\classes\cn\ccnu\classpath 
         *      即:/javaee/WEB-INF/classes/cn/ccnu/classpath 
         */  
        InputStream in = ClassServlet.class.getResourceAsStream("b.properties");  
        Properties porp = new Properties();  
        porp.load(in);  
        System.out.println(porp.getProperty("url"));  
  
    }  
  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        doGet(request, response);  
    }  
  
}

四、路径

这里的路径仅仅是指web.xml文件中的标签内容。这个路径是虚拟的路径,只有相对路径(相对于当前应用的路径),但相对路径的写法可以是精确查询和模糊查询。

要么以“*”开关,要么为“/”开头,当通常情况看下,我们都会以"/"开头。

<!—精确查询,是相对于当前应用-->
<url-pattern>/servlet/testPath</url-pattern>

<!—模糊查询,表示匹配servlet目录下的所有文件或请求。/*表示匹配所有-->
<url-pattern>/servlet/*</url-pattern>

<!—模糊查询,表示匹配所有后缀名为do的文件或请求-->
<url-pattern>*.do</url-pattern>