javaweb中路径的书写总结

本文总结了JavaWeb项目中各类路径的书写方式,包括客户端路径(超链接、表单提交)、服务器端路径(转发、包含)、资源获取路径及<url-pattern>路径,并详细解释了各种路径的区别和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文是转载文章,文章的来源:优快云博客

博主:半醉看夕阳

文章:javaweb中路径的书写总结

博文地址:http://blog.youkuaiyun.com/bbb695480667/article/details/53838321


在写javaweb项目的时候,总会遇到路径书写的问题,现在将其作个总结。

        在javaweb中需要书写路径的地方主要有这四大类:

        客服端路径

                超链接

                表当

                重定向

        服务器端路径

                转发

                包含

        资源获取路径

                servletContext获取资源

                ClassLoader获取资源

                Class获取资源

        <url-pattern>路径

现分别作介绍

其构建的javaweb如下:


1客服端路径

A超链接

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>页面A</title>  
  8. </head>  
  9. <body>  
  10.     <!--   
  11.         超链接和表当都有三种书写路径的方式  
  12.             1,绝对地址    
  13.             2,以"/"开头的相对地址  
  14.             3,不以"/"开头的相对地址   
  15.     -->  
  16.       
  17.     <!-- 1.绝对地址   -->  
  18.         <!-- 完整的URL -->  
  19.     <a href="http://localhost:8080/javaee/jsp/b.jsp">这是绝对地址超链接</a><br/>  
  20.       
  21.     <!-- 2.以"/"开头的相对地址    -->  
  22.         <!-- /代表了整个web项目,即:http://localhost:8080/ -->  
  23.     <a href="/javaee/jsp/b.jsp">这是以"/"开头的相对地址超链接</a><br/>  
  24.       
  25.     <!-- 3.不以"/"开头的相对地址   -->  
  26.         <!--   
  27.             不以/开头,则相对于当前资源的路径  
  28.             当前资源的路径为:http://localhost:8080/javaee/jsp/  
  29.             而b.jsp也在此路径下  
  30.             所以直接书写b.jsp  
  31.          -->  
  32.     <a href="b.jsp">这是不以"/"开头的相对地址超链接</a><br/>  
  33. </body>  
  34. </html>  

B表单

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <!-- 所有的表单都是提交到b.jsp -->  
  11.     <!--   
  12.         表当提交路径有三种书写方式   
  13.             1,绝对地址    
  14.             2,以"/"开头的相对地址  
  15.             3,不以"/"开头的相对地址   
  16.     -->  
  17.     <form action="http://localhost:8080/javaee/jsp/b.jsp" methoe="get">  
  18.         username:<input type="text" name="username" value="">  
  19.         <input type="submit" value="提交---绝对地址    ">  
  20.     </form>  
  21.     <!--   
  22.         以/开头的相对地址  
  23.           
  24.         此时的/代表整个web项目,即:http://localhost:8080/  
  25.     -->  
  26.     <form action="/javaee/jsp/b.jsp" methoe="get">  
  27.         username:<input type="text" name="username" value="">  
  28.         <input type="submit" value="提交---以/开头的相对地址">  
  29.     </form>  
  30.       
  31.     <form action="b.jsp" methoe="get">  
  32.         username:<input type="text" name="username" value="">  
  33.         <input type="submit" value="提交---不以/开头的相对地址 ">  
  34.     </form>  
  35.       
  36.     <!-- 表单提交到Servlet -->  
  37.     <!--   
  38.         表单提交到Servlet有三种书写方式  
  39.         1,绝对路径  
  40.         2,以"/"开头的相对地址  
  41.         3,不以"/"开头的相对地址   
  42.     -->  
  43.     <!-- 1.绝对地址   -->  
  44.         <!-- 完整的URL -->  
  45.     <form action="http://localhost:8080/javaee/PathServlet" methoe="get">  
  46.         username:<input type="text" name="username" value="">  
  47.         <input type="submit" value="表单提交到Servlet---绝对地址">  
  48.     </form>  
  49.       
  50.     <!-- 2.以/开头的相对地址  -->  
  51.         <!-- 此时的/代表整个web项目,即:http://localhost:8080/  -->  
  52.     <form action="/javaee/PathServlet" methoe="get">  
  53.         username:<input type="text" name="username" value="">  
  54.         <input type="submit" value="表单提交到Servlet---以/开头的相对地址">  
  55.     </form>  
  56.     <!-- 3.不以/开头的相对地址    -->  
  57.         <!--   
  58.             不以/开头的相对路径是相对于当前资源的路径  
  59.             此时form.jsp的地址为:http://localhost:8080/javaee/jsp/form.jsp  
  60.             所以当前资源路径为:http://localhost:8080/javaee/jsp  
  61.             而要提交的Servlet的路径为Http://localhost:8080/javaee/PathServlet  
  62.             所以路径为当前路径的上一级路径下  
  63.             即路径为:../PathServlet  
  64.             注:.代表当前路径  
  65.                ..代表当前路径的上一级路径  
  66.         -->  
  67.     <form action="../PathServlet" methoe="get">  
  68.         username:<input type="text" name="username" value="">  
  69.         <input type="submit" value="表单提交到Servlet---不以/开头的相对地址">  
  70.     </form>  
  71. </body>  
  72. </html>  

C重定向

[java]  view plain  copy
  1. package cn.ccnu.path;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. /* 
  10.  * 重定向有三种路径书写方式 
  11.  *      1.绝对路径 
  12.  *      2.以"/"开头的相对路径 
  13.  *      3.不以"/"开头的相对路径 
  14.  */  
  15. public class RedirectServlet extends HttpServlet {  
  16.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  17.             throws ServletException, IOException {  
  18.         response.sendRedirect("http://localhost:8080/javaee/jsp/b.jsp");  
  19.         /* 
  20.          * 2.以"/"开头的相对路径 
  21.          *      此时,/代表整个web工程的路径,即http://localhost:8080/ 
  22.          */  
  23. //      response.sendRedirect("/javaee/jsp/b.jsp");  
  24.         /* 
  25.          * 3.不以"/"开头的相对路径 
  26.          *      此时是相对于当前资源的相对路径 
  27.          *      当前资源路径为:http://localhost:8080/javaee/RedirectServlet 
  28.          *      即表示:RedirectServlet在路径http://localhost:8080/javaee之下 
  29.          *      而b.jsp在http://localhost:8080/javaee/jsp/b.jsp 
  30.          *      所以最终地址写为:jsp/b.jsp 
  31.          */  
  32. //      response.sendRedirect("jsp/b.jsp");  
  33.           
  34.     }  
  35.   
  36.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  37.             throws ServletException, IOException {  
  38.         doGet(request, response);  
  39.     }  
  40.   
  41. }  

2服务器端路径

A请求转发

[java]  view plain  copy
  1. package cn.ccnu.path;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. /* 
  10.  * 服务器端的路径不能是绝对路径,只能是相对路径,也分为以/开头和不以/开头两种 
  11.  *      1.以"/"开头的相对路径 
  12.  *      2.不以"/"开头的相对路径 
  13.  */  
  14. public class DispatcherServlet extends HttpServlet {  
  15.   
  16.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  17.             throws ServletException, IOException {  
  18.         /* 
  19.          * 1.以"/"开头的相对路径 
  20.          *      此时,/代表当前web项目,即:http://localhost:8080/javaee 
  21.          */  
  22. //      request.getRequestDispatcher("/jsp/b.jsp").forward(request, response);  
  23.         /* 
  24.          * 2.不以"/"开头的相对路径 
  25.          *      相对于当前资源的相对路径 
  26.          *  此时,当前资源的路径为:http://localhost:8080/javaee/DispatcherServlet 
  27.          *  所以要转发去的资源的路径以:http://localhost:8080/javaee开头 
  28.          */  
  29.         request.getRequestDispatcher("jsp/b.jsp").forward(request, response);  
  30.     }  
  31.   
  32.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  33.             throws ServletException, IOException {  
  34.         doGet(request, response);  
  35.     }  
  36.   
  37. }  

B请求包含

[java]  view plain  copy
  1. package cn.ccnu.path;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. /* 
  10.  * 请求包含不能书写绝对地址,只能书写相对地址 
  11.  *      1.以"/"开头的相对路径 
  12.  *      2.不以"/"开头的相对路径 
  13.  *  
  14.  */  
  15. public class IncludeServlet extends HttpServlet {  
  16.   
  17.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  18.             throws ServletException, IOException {  
  19.         /* 
  20.          * 1.以"/"开头的相对路径 
  21.          *      此时,/代表当前web项目,即:http://localhost:8080/javaee 
  22.          */  
  23. //      request.getRequestDispatcher("/jsp/b.jsp").include(request, response);  
  24.         /* 
  25.          * 2.不以"/"开头的相对路径 
  26.          *      相对于当前资源的相对路径 
  27.          *  此时,当前资源的路径为:http://localhost:8080/javaee/IncludeServlet 
  28.          *  所以要转发去的资源的路径以:http://localhost:8080/javaee开头 
  29.          */  
  30.         request.getRequestDispatcher("jsp/b.jsp").include(request, response);  
  31.     }  
  32.   
  33.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  34.             throws ServletException, IOException {  
  35.         doGet(request, response);  
  36.     }  
  37.   
  38. }  

3资源获取路径

AServletContext获取资源

备注:此处的ServletContext获取资源是错误的说法

[java]  view plain  copy
  1. package cn.ccnu.path;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. /* 
  12.  * ServletContext获取资源必须是相对路径,不能是绝对路径,但不管是以/开头,还是不以/开头, 
  13.  * 都是相对于当前资源的相对路径 
  14.  *  
  15.  */  
  16. public class ServletContextServlet extends HttpServlet {  
  17.   
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         String path1 = this.getServletContext().getRealPath("/a.properties");  
  21.         String path2 = this.getServletContext().getRealPath("a.properties");  
  22.         System.out.println(path1);  
  23.         System.out.println(path2);  
  24.         //输出的地址一样  
  25.     }  
  26.   
  27.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  28.             throws ServletException, IOException {  
  29.         doGet(request, response);  
  30.     }  
  31.   
  32. }  

备注:此处的ServletContext获取资源是错误的说法。

本人转载后,进行了如下修改


ServletContext获取资源

[java]  view plain  copy
  1. package cn.ccnu.path;    
  2.     
  3. import java.io.IOException;    
  4. import javax.servlet.ServletException;    
  5. import javax.servlet.http.HttpServlet;    
  6. import javax.servlet.http.HttpServletRequest;    
  7. import javax.servlet.http.HttpServletResponse;    
  8. /*  
  9.  * ServletContext接口的getRealPath(Stringpath)方法 
  10.  * 返回的是资源文件在服务器文件系统上的真实路径(带有盘符)。 
  11.  * 参数path代表资源文件的虚拟路径,它应该以正斜线(/)开始,"/"表示当前web应用的根目录,也可以不以"/"开始。 
  12.  */    
  13. public class ServletContextServlet extends HttpServlet {    
  14.     
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)    
  16.             throws ServletException, IOException {    
  17.         //当前资源:ServletContextServlet 和a.properties是在同一目录下的,  
  18.         //   即:都是类路径【\javaee\WEB-INF\classes】下的  
  19.         //    \javaee\WEB-INF\classes\cn\ccnu\path 下的同级文件  
  20.           
  21.         String path1 = this.getServletContext().getRealPath("/a.properties");//可以显示,但是路径错误    
  22.         //路径错误的言外之意:a.properties文件的路径不是在:E:\apache-tomcat-7.0.73\webapps\javaee\a.properties  
  23.         //说明,我们写的方式存在问题!!!!!!  
  24.         String path2 = this.getServletContext().getRealPath("a.properties");  
  25.         //可以显示,但是路径错误  (原因:与path1同)  
  26.           
  27.         //正确:  
  28.         //原因:因为a.properties文件实在cn.ccnu.path包下的,而这些java包都是在src文件夹下的。  
  29.         //而src文件夹下的文件,编译后,都是存放在   /WEB-INF/classes 目录下,所以,要显示出a.properties文件的正确路径,  
  30.         //应该把它所在的cn.ccnu.path写上,因为这样才是它的真实存放路径  
  31.         String path3 = this.getServletContext().getRealPath("/WEB-INF/classes/cn/ccnu/path/a.properties");  
  32.          
  33.         String path4 = this.getServletContext().getRealPath("");//当前web应用的路径  
  34.           
  35.         String path5 = this.getServletContext().getRealPath("c.properties");  
  36.         //可以显示,但是路径错误  (原因:与path1同)  
  37.           
  38.         //不存在的文件演示path6    
  39.         String path6 = this.getServletContext().getRealPath("2333333.txt");//错误  
  40.         //无论你的文件存在还是不存在,他都会给你是显示出路径来,所以写路径时,应该注意,一定要真实存在的文件,然后写出它所在的正确路径  
  41.           
  42.         System.out.println(path1);    
  43.         //   E:\apache-tomcat-7.0.73\webapps\javaee\a.properties  
  44.         System.out.println(path2); //输出的地址一样    
  45.         //   E:\apache-tomcat-7.0.73\webapps\javaee\a.properties  
  46.           
  47.         System.out.println(path3);   
  48.         // E:\apache-tomcat-7.0.73\webapps\javaee\WEB-INF\classes\cn\ccnu\path\a.properties  
  49.         System.out.println(path4);  
  50.        // E:\apache-tomcat-7.0.73\webapps\javaee  
  51.         System.out.println(path5);  
  52.         //  E:\apache-tomcat-7.0.73\webapps\javaee\c.properties  
  53.         System.out.println(path6);  
  54.         // E:\apache-tomcat-7.0.73\webapps\javaee\2333333.txt  
  55.           
  56.     }    
  57.     
  58.     public void doPost(HttpServletRequest request, HttpServletResponse response)    
  59.             throws ServletException, IOException {    
  60.         doGet(request, response);    
  61.     }    
  62.     
  63. }    

以上,就是本人转载后进行的修改



BClassLoader获取资源

[java]  view plain  copy
  1. package cn.ccnu.classloaderpath;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. /* 
  12.  * ClassLoader类加载器不能通过绝对地址来加载资源,只能通过相对地址来加载资源 
  13.  * 但相对地址不管前面加不加/都是相当于类路径的相对地址 
  14.  *  
  15.  */  
  16. public class ClassLoaderServlet extends HttpServlet {  
  17.   
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         /* 
  21.          * 加了/,其地址是相对于类路径的相对地址 
  22.          */  
  23. //      InputStream in = this.getClass().getClassLoader().getResourceAsStream("/cn/ccnu/classloaderpath/c.properties");  
  24. //      Properties prop = new Properties();  
  25. //      prop.load(in);  
  26. //      System.out.println(prop.getProperty("url"));  
  27.           
  28.         /* 
  29.          * 不加/,其地址是相对于类路径的相对地址 
  30.          */  
  31.         InputStream in = this.getClass().getClassLoader().getResourceAsStream("cn/ccnu/classloaderpath/c.properties");  
  32.         Properties prop = new Properties();  
  33.         prop.load(in);  
  34.         System.out.println(prop.getProperty("url"));  
  35.         /* 
  36.          * 总结:不能使用绝对地址,而只能只用相对地址 
  37.          * 且不管加不加/的相对地址,都是相对于类路径的相对地址 
  38.          *  
  39.          */  
  40.     }  
  41.   
  42.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  43.             throws ServletException, IOException {  
  44.         doGet(request, response);  
  45.     }  
  46.   
  47. }  

CClass获取资源

[java]  view plain  copy
  1. package cn.ccnu.classpath;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. /* 
  12.  * Class读取资源不能是绝对路径,只能是相对路径,又分为以/开头或者是不以/开头 
  13.  *      1.以/开头的相对路径 
  14.  *      2.不以/开头的相对路径 
  15.  */  
  16. public class ClassServlet extends HttpServlet {  
  17.   
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  19.             throws ServletException, IOException {  
  20.         /* 
  21.          * 1.以/开头的相对路径 
  22.          *      此时的/代表类路径,即:/javaee/WEB-INF/classes 
  23.          */  
  24. //      InputStream in = ClassServlet.class.getResourceAsStream("/cn/ccnu/classpath/b.properties");  
  25. //      Properties porp = new Properties();  
  26. //      porp.load(in);  
  27. //      System.out.println(porp.getProperty("url"));  
  28.         /* 
  29.          * 2.不以/开头的相对路径 
  30.          *      此时相对的是:类ClassServlet.class的路径,即:\javaee\WEB-INF\classes\cn\ccnu\classpath 
  31.          *      即:/javaee/WEB-INF/classes/cn/ccnu/classpath 
  32.          */  
  33.         InputStream in = ClassServlet.class.getResourceAsStream("b.properties");  
  34.         Properties porp = new Properties();  
  35.         porp.load(in);  
  36.         System.out.println(porp.getProperty("url"));  
  37.   
  38.     }  
  39.   
  40.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  41.             throws ServletException, IOException {  
  42.         doGet(request, response);  
  43.     }  
  44.   
  45. }  

4<url-pattern>路径

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值