JavaServlet 路径书写总结

  在写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.     <href="http://localhost:8080/javaee/jsp/b.jsp">这是绝对地址超链接</a><br/>  
  20.       
  21.     <!-- 2.以"/"开头的相对地址    -->  
  22.         <!-- /代表了整个web项目,即:http://localhost:8080/ -->  
  23.     <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.     <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获取资源

[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. }  

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>路径

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

                Servlet的路径跳转

一、JSP跳转到Servlet

1、相对路径,如href="servlet/TestServlet" 
如果写成"/servlet/TestServlet"会报错,因为第一个“/”表示的是【服务器根目录】

2、绝对路径,通过内置成员变量path实现,如href="<%=path%>/servlet/TestServlet"
path得到的是项目根目录,如【http://localhost:8080/ServletDemo】
二、Servlet跳转JSP
1、请求重定向:response.sendRedirect(request.getContextPath()+"/xxx.jsp");
request.getContextPath()获得项目根目录,或者通过"../xxx.jsp"取得上层路径得到

2、服务器内部转发:
request.getRequestDispatcher("../xxx.jsp").forward(req,resp);
request.getRequestDispatcher("/test.jsp").forward(request, response); //斜线表示项目的根目录
小结:Servlet都可以通过../xxx.jsp获取路径
三、web.xml的路径
 

web.xml的<url-pattern>必须以“/”开头,“/”表示项目的根目录

在JSP页面上使用相对路径和绝对路径调用servlet

web.xml的<url-pattern>必须以“/”开头,“/”表示项目的根目录
如果在Servlet中使用请求重定向方式跳转到其他jsp页面,则需要:
response.sendRedirect(request.getContextPath()+"/test.jsp");
服务器内部跳转路径:
request.getRequestDispatcher("/test.jsp").forward(request,response);//这里的斜线表示项目的根目录
或者request.getRequestDispatcher("../test.jsp").forward(request,response);//“..”表示回到上层目录也就是根目录;
PS:如果<url-pattern>带一层路径 如:<url-pattern>/servlet/loginServlet</url-pattern>,则内部转发的时会从WebRoot/servlet/路径下找jsp页面,如果要转发的页面不在WebRoot/servlet/路径下,则需要“..”回到上层或根目录再定位到jsp页面,如下:request.getRequestDispatcher("../test.jsp").forward(request,response);
如果要转发的页面在WebRoot/servlet/路径下,则如下:request.getRequestDispatcher("/test.jsp").forward(request,response);

猜你喜欢

转载自www.cnblogs.com/798911215-Darryl-Tang/p/9141720.html
今日推荐