javaweb教程 javaweb项目路径总结

javaweb源代码目录与服务器中javaweb目录对比

  • javaweb的源代码目录
|--- pom.xml
|--- src
      |--- main
			|--- java
			|--- source
	  |--- test
|--- web
	  |--- WEB-INF
			 |--- web.xml
  • javaweb编译打包之后发布到服务器中的目录
|--- WEB-INF
		|--- jsp
			  |--- index.jsp			  	
 		|--- classes
 		      |--- java源文件编译存放的目录	
		|--- web.xml 
|--- index.html
  • 关于javaweb的源代码目录 与 打包发布的目录转换
    在这里插入图片描述
    • 源码工程中java目录中所有.java源码被编译到了 发布包中的classes目录。(java目录中的资源文件不会被拷贝到classes目录中,但是如果必须把资源文件放到java目录中,可以使用maven插件拷贝java目录中的资源文件。)
    • 源码工程中的Web目录,被编译到了项目的根目录。源码Web目录中资源文件被编译到项目的根目录,然后源码Web目录中中的WEB-INF内的文件,被拷贝到了打包后的WEB-INF中。



发布后的项目根路径获取

  • 获取项目的根路径 (指的是web的上下文映射路径)
    这个不是真实的磁盘路径,而是用于url上的虚拟映射路径。
#项目的根路径. 这个不是真实的磁盘路径,而是用于url上的虚拟映射路径。
http://localhost:8080/test/hello 就是指的/test
String path = req.getServletContext().getContextPath();
System.out.println(path);
  • 获取当前的servlet的映射路径
http://localhost:8080/test/hello 就是指的/hello
String servletPath = req.getServletPath();
System.out.println(path)
  • 获取当前项目的磁盘路径(用于读取文件)
#然后凭借项目根路径的磁盘路径
String filePath = req.getServletContext().getRealPath(”“);
System.out.println(filePath);



获取URL请求的静态资源的真实路径

  • 获取静态资源的URI,就是相对于项目的。而servletContext.getRealPath("")可以获取到项目根路径的磁盘路径。所以可以将URI对应的资源,转发成磁盘上该资源的路径。
  • getContextPath() 与 getRealPath()没有半毛钱关系。且互相不会影响。
#比如 http://www.baidu.com/test/index.html
#1. 先获取URL上的URI,即/test/index.html。
#2. 然后去除掉getContextPath()的虚拟映射路径,则剩下index.html.表示位于项目的根目录下。
#3. 然后servletContext.getRealPath("/index.html"),就能获取到该文件的真实路径了。

#获取路径
String filePath = req.getServletContext().getRealPath("/index.html");

#读取路径下的资源
InputStream inputStream =  req.getServletContext().getResourceAsStream("/index.html");



项目的类路径获取与加载类路径下资源

项目的类路径,就是指 “项目/WEB-INF/classes” 的路径

  • 通过普通类的classLoader获取项目的类路径。
#获取classes的磁盘路径。
URL url = HelloServlet.class.getClassLoader().getResource("");
String classesPath = url.getFile();

#获取classes下的磁盘路径,然后拼装路径,获取classes下的资源文件。
Properties properties = new Properties();
properties.load(new FileInputStream(new File(url.getFile()+"/a.properties")));
System.out.println(properties.get("name"));

#当然,你也可以直接使用如下方法。(相对于classess路径)
InputStream inputStream =  HelloServlet.class.getClassLoader().getResourceAsStream("/a.properties");
properties.load(inputStream);
System.out.println(properties.get("name"));
  • 通过获取项目的磁盘路径,并且拼装/WEB-INF/classes
#获取项目的磁盘路径。
ServletContext servletContext = req.getServletContext();
#根据项目的磁盘路径,拼装出classes下的路劲。
String classesPath = servletContext.getRealPath("")+"/WEB-INF/classes";
System.out.println(classesPath);

#获取classes目录下的资源。
Properties properties = new Properties();
properties.load(new FileInputStream(new File(classesPath+"/a.properties")));
System.out.println(properties.get("name"));



重定向与请求转发的路径使用

  • 相对路径

    • 服务器中设置重定向,或者请求转发的相对路径,是相对于当前class文件所在的路径。不推荐使用,容易出现错误。

  • 绝对路径: (推荐使用)
    *请求转发的绝对路径是 相对于 “域名/服务器的根路径” 的位置

    #跳转的路径就是 http://localhost:8080/test/index.html
    #请求转发使用绝对路径,所以是相对于http://localhost:8080/test/路径。
    
    RequestDispatcher requestDispatcher = req.getRequestDispatcher("/index.html");
    requestDispatcher.forward(req, resp);
    
    • 重定向的绝对路径是相对于域名的。(不包含服务器的根路径)
    #跳转的路径就是 http://localhost:8080/test/index.html
    #重定向使用绝对路径,所以是相对于http://localhost:8080/路径。
    
    #需要自己加上一个项目根路径。
    resp.sendRedirect( req.getServletContext().getContextPath()+"/index.html");
    

  • url路径

#重定向支持 http,https协议开头的url跳转
resp.sendRedirect( "http://www.baidu.com");



浏览器中html中的路径使用

  • 绝对路径:
    浏览器中的绝对路径,是相对于域名的。
#<a>标签 设置绝对路径之后,访问的URL为: http://localhost:8080/hello2
<a href="/hello2">这是一个跳转连接</a>	

#<form>表单
<form method="POST" action="/hello3">
	<input type="submit" value="提交">
</form>
  • 相对路径
    浏览器中的相对路径,默认是相对于当前页面。容易出错误,不推荐使用。

  • <base>的相对路径
    浏览器在head中可以指定一个<base>标签,指定相对路径的基准路径。用于解决绝对路径不能带有应用上下文的问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base href="http://localhost:8080/test/">
</head>
<body>
    <a href="./hello2">这是一个锚点</a>
    <form action="/hello3" method="post">
        <input type="submit" value="点击提交">
    </form>
</body>
</html>



获取classes下的properties配置文件

#a.properties文件必须放在 classes 目录下。
InputStream inputStream =  HelloServlet.class.getClassLoader().getResourceAsStream("/a.properties");
properties.load(inputStream);
System.out.println(properties.get("name"));
发布了58 篇原创文章 · 获赞 34 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_36723759/article/details/104164075