JavaWeb中路径总结

Web中的相对路径和绝对路径

在 javaWeb 中,路径分为相对路径和绝对路径两种:

  • 相对路径是:(相对于当前资源所在的路径为标准,不以/开始的路径就是相对路径)

      . 		表示当前目录
      .. 		表示上一级目录
      资源名 	表示当前目录/资源名(和资源名前加./是等价的)
    
  • 绝对路径:

      http://ip:port/工程路径/资源路径
    

    在实际开发中,路径都使用绝对路径,而不简单的使用相对路径。

      1、绝对路径
      2、base+相对
    

web中 /斜杠 的不同意义

  • 在web中/斜杠是一种绝对路径。

      浏览器解析:
      	/ 斜杠如果被浏览器解析,得到的地址是:http://ip:port/
      	1、<a href="/">斜杠</a>
      	2、response.sendRediect("/"); 把斜杠发送给浏览器解析。得到 http://ip:port/
      	3、只要是html标签(详见html标签列表)里面写的路径都是浏览器解析。除此之外都是服务器解析;  
      	
      
      服务器解析:
      	/ 斜杠 如果被服务器解析,得到的地址是:http://ip:port/工程路径
      	1、<url-pattern>/servlet1</url-pattern>
      	2、servletContext.getRealPath("/");
      	3、request.getRequestDispatcher("/");
      	4、<jsp:forward></jsp:forward>(jsp自定义的标签;目前见到的所有有前缀的标签都是服务器解析的)
    

以后推荐写绝对路径

	ServletContextListener{
			context.setAttribute("ctp",request.getContextPath());//在监听器中设置
	}
	<a href="<%=request.getContextPath()%>/index.jsp">hello</a>
	<a href="${ctp}/index.jsp">hello</a>

idea和eclipse中的相对路径

  • IDEA中:
    如果使用JUnit中的单元测试方法测试,相对路径即为当前Module下。
    如果使用main()测试,相对路径即为当前的Project下。

  • Eclipse中:
    不管使用单元测试方法还是使用main()测试,相对路径都是当前的Project下。

与路径有关的方法

ServletContext 和路径相关的方法:

public class ContextServlet extends HttpServlet {
    
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //获取ServletContext对象
        ServletContext context = getServletConfig().getServletContext();
//        2、获取当前的工程路径,格式: /工程路径
        System.out.println("当前工程路径:"+context.getContextPath());//"/06_servlet"
//        3、获取工程部署后在服务器硬盘上的绝对路径
//        '/' 斜杠被服务器解析地址为:http://ip:port/工程名/  映射到IDEA代码的web目录
        System.out.println("工程部署的路径是:"+context.getRealPath("/"));//E:\idea_workspace\shangguigu_JavaWeb\out\artifacts\06_servlet_war_exploded\
        System.out.println("工程下img目录1.jpg的绝对路径是:"+context.getRealPath("/img/1.jpg"));
    }
}

HttpServletRequest和路径相关的方法:

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
//      getRequestURI()					获取请求的资源路径
      System.out.println("URI="+req.getRequestURI());//"/07_servlet/RequestAPIServlet"
//      getRequestURL()					获取请求的统一资源定位符(绝对路径)
      System.out.println("URL="+req.getRequestURL());//"http://localhost:8080/07_servlet/RequestAPIServlet"

//      getRemoteHost()				    获取客户端的ip地址
      /**
       * 在IDEA中,使用localhost访问时,得到的客户端 ip 地址是 ===>>> 127.0.0.1<br/>
       * 在IDEA中,使用127.0.0.1访问时,得到的客户端 ip 地址是 ===>>> 127.0.0.1<br/>
       * 在IDEA中,使用 真实ip 访问时,得到的客户端 ip 地址是 ===>>> 真实的客户端 ip 地址<br/>
       */
      System.out.println("客户端ip地址:"+req.getRemoteHost());

      //getContextPath()返回获取当前工程路径
      System.out.println("获取当前工程路径:"+req.getContextPath());// “/07_servlet”
	}

base标签的作用

  • 相对路径在工作时候都会参照当前浏览器地址栏中的地址来进行跳转。

  • base标签可以设置当前页面中所有相对路径工作时,参照哪个路径来进行跳转

没有设置base标签:(此时页面中的超链接需要根据浏览器地址栏动态的改变)
在这里插入图片描述
设置base标签之后:
在这里插入图片描述
在这里插入图片描述

好的博客:
JavaWeb(七)之详解JavaWeb路径

猜你喜欢

转载自blog.csdn.net/weixin_44630656/article/details/114170600