本文是转载文章,文章的来源:优快云博客
博主:半醉看夕阳
文章:javaweb中路径的书写总结
博文地址:http://blog.youkuaiyun.com/bbb695480667/article/details/53838321
在写javaweb项目的时候,总会遇到路径书写的问题,现在将其作个总结。
在javaweb中需要书写路径的地方主要有这四大类:
客服端路径
超链接
表当
重定向
服务器端路径
转发
包含
资源获取路径
servletContext获取资源
ClassLoader获取资源
Class获取资源
<url-pattern>路径
现分别作介绍
其构建的javaweb如下:
1客服端路径
A超链接
- <%@ 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>
B表单
- <%@ 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>Insert title here</title>
- </head>
- <body>
- <!-- 所有的表单都是提交到b.jsp -->
- <!--
- 表当提交路径有三种书写方式
- 1,绝对地址
- 2,以"/"开头的相对地址
- 3,不以"/"开头的相对地址
- -->
- <form action="http://localhost:8080/javaee/jsp/b.jsp" methoe="get">
- username:<input type="text" name="username" value="">
- <input type="submit" value="提交---绝对地址 ">
- </form>
- <!--
- 以/开头的相对地址
- 此时的/代表整个web项目,即:http://localhost:8080/
- -->
- <form action="/javaee/jsp/b.jsp" methoe="get">
- username:<input type="text" name="username" value="">
- <input type="submit" value="提交---以/开头的相对地址">
- </form>
- <form action="b.jsp" methoe="get">
- username:<input type="text" name="username" value="">
- <input type="submit" value="提交---不以/开头的相对地址 ">
- </form>
- <!-- 表单提交到Servlet -->
- <!--
- 表单提交到Servlet有三种书写方式
- 1,绝对路径
- 2,以"/"开头的相对地址
- 3,不以"/"开头的相对地址
- -->
- <!-- 1.绝对地址 -->
- <!-- 完整的URL -->
- <form action="http://localhost:8080/javaee/PathServlet" methoe="get">
- username:<input type="text" name="username" value="">
- <input type="submit" value="表单提交到Servlet---绝对地址">
- </form>
- <!-- 2.以/开头的相对地址 -->
- <!-- 此时的/代表整个web项目,即:http://localhost:8080/ -->
- <form action="/javaee/PathServlet" methoe="get">
- username:<input type="text" name="username" value="">
- <input type="submit" value="表单提交到Servlet---以/开头的相对地址">
- </form>
- <!-- 3.不以/开头的相对地址 -->
- <!--
- 不以/开头的相对路径是相对于当前资源的路径
- 此时form.jsp的地址为:http://localhost:8080/javaee/jsp/form.jsp
- 所以当前资源路径为:http://localhost:8080/javaee/jsp
- 而要提交的Servlet的路径为Http://localhost:8080/javaee/PathServlet
- 所以路径为当前路径的上一级路径下
- 即路径为:../PathServlet
- 注:.代表当前路径
- ..代表当前路径的上一级路径
- -->
- <form action="../PathServlet" methoe="get">
- username:<input type="text" name="username" value="">
- <input type="submit" value="表单提交到Servlet---不以/开头的相对地址">
- </form>
- </body>
- </html>
C重定向
- package cn.ccnu.path;
- 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.以"/"开头的相对路径
- * 3.不以"/"开头的相对路径
- */
- public class RedirectServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.sendRedirect("http://localhost:8080/javaee/jsp/b.jsp");
- /*
- * 2.以"/"开头的相对路径
- * 此时,/代表整个web工程的路径,即http://localhost:8080/
- */
- // response.sendRedirect("/javaee/jsp/b.jsp");
- /*
- * 3.不以"/"开头的相对路径
- * 此时是相对于当前资源的相对路径
- * 当前资源路径为:http://localhost:8080/javaee/RedirectServlet
- * 即表示:RedirectServlet在路径http://localhost:8080/javaee之下
- * 而b.jsp在http://localhost:8080/javaee/jsp/b.jsp
- * 所以最终地址写为:jsp/b.jsp
- */
- // response.sendRedirect("jsp/b.jsp");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
2服务器端路径
A请求转发
- package cn.ccnu.path;
- 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);
- }
- }
B请求包含
- package cn.ccnu.path;
- 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 IncludeServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- /*
- * 1.以"/"开头的相对路径
- * 此时,/代表当前web项目,即:http://localhost:8080/javaee
- */
- // request.getRequestDispatcher("/jsp/b.jsp").include(request, response);
- /*
- * 2.不以"/"开头的相对路径
- * 相对于当前资源的相对路径
- * 此时,当前资源的路径为:http://localhost:8080/javaee/IncludeServlet
- * 所以要转发去的资源的路径以:http://localhost:8080/javaee开头
- */
- request.getRequestDispatcher("jsp/b.jsp").include(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
3资源获取路径
AServletContext获取资源
备注:此处的ServletContext获取资源是错误的说法
- package cn.ccnu.path;
- 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;
- /*
- * ServletContext获取资源必须是相对路径,不能是绝对路径,但不管是以/开头,还是不以/开头,
- * 都是相对于当前资源的相对路径
- *
- */
- 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);
- }
- }
备注:此处的ServletContext获取资源是错误的说法。
本人转载后,进行了如下修改
ServletContext获取资源
- package cn.ccnu.path;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /*
- * ServletContext接口的getRealPath(Stringpath)方法
- * 返回的是资源文件在服务器文件系统上的真实路径(带有盘符)。
- * 参数path代表资源文件的虚拟路径,它应该以正斜线(/)开始,"/"表示当前web应用的根目录,也可以不以"/"开始。
- */
- public class ServletContextServlet extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- //当前资源:ServletContextServlet 和a.properties是在同一目录下的,
- // 即:都是类路径【\javaee\WEB-INF\classes】下的
- // \javaee\WEB-INF\classes\cn\ccnu\path 下的同级文件
- String path1 = this.getServletContext().getRealPath("/a.properties");//可以显示,但是路径错误
- //路径错误的言外之意:a.properties文件的路径不是在:E:\apache-tomcat-7.0.73\webapps\javaee\a.properties
- //说明,我们写的方式存在问题!!!!!!
- String path2 = this.getServletContext().getRealPath("a.properties");
- //可以显示,但是路径错误 (原因:与path1同)
- //正确:
- //原因:因为a.properties文件实在cn.ccnu.path包下的,而这些java包都是在src文件夹下的。
- //而src文件夹下的文件,编译后,都是存放在 /WEB-INF/classes 目录下,所以,要显示出a.properties文件的正确路径,
- //应该把它所在的cn.ccnu.path写上,因为这样才是它的真实存放路径
- String path3 = this.getServletContext().getRealPath("/WEB-INF/classes/cn/ccnu/path/a.properties");
- String path4 = this.getServletContext().getRealPath("");//当前web应用的路径
- String path5 = this.getServletContext().getRealPath("c.properties");
- //可以显示,但是路径错误 (原因:与path1同)
- //不存在的文件演示path6
- String path6 = this.getServletContext().getRealPath("2333333.txt");//错误
- //无论你的文件存在还是不存在,他都会给你是显示出路径来,所以写路径时,应该注意,一定要真实存在的文件,然后写出它所在的正确路径
- System.out.println(path1);
- // E:\apache-tomcat-7.0.73\webapps\javaee\a.properties
- System.out.println(path2); //输出的地址一样
- // E:\apache-tomcat-7.0.73\webapps\javaee\a.properties
- System.out.println(path3);
- // E:\apache-tomcat-7.0.73\webapps\javaee\WEB-INF\classes\cn\ccnu\path\a.properties
- System.out.println(path4);
- // E:\apache-tomcat-7.0.73\webapps\javaee
- System.out.println(path5);
- // E:\apache-tomcat-7.0.73\webapps\javaee\c.properties
- System.out.println(path6);
- // E:\apache-tomcat-7.0.73\webapps\javaee\2333333.txt
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
以上,就是本人转载后进行的修改
BClassLoader获取资源
- package cn.ccnu.classloaderpath;
- 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;
- /*
- * 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);
- }
- }
CClass获取资源
- package cn.ccnu.classpath;
- 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;
- /*
- * Class读取资源不能是绝对路径,只能是相对路径,又分为以/开头或者是不以/开头
- * 1.以/开头的相对路径
- * 2.不以/开头的相对路径
- */
- 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);
- }
- }
4<url-pattern>路径
要么以“*”开关,要么为“/”开头,当通常情况看下,我们都会以"/"开头。