zoukankan      html  css  js  c++  java
  • 【java】web项目中各种路径的获取

    以工程名为/DemoWeb为例:
    访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp

    1 JSP中获得当前应用的相对路径和绝对路径
    (1)得到工程名:request.getContextPath()
    结果:/DemoWeb

    (2)得到包含工程名的当前页面全路径:request.getRequestURI()
    结果:/DemoWeb/test/testpath.jsp

    (3)得到IE地址栏地址:request.getRequestURL()
    结果:http://localhost:8080/DemoWeb/test/testpath.jsp


    (4)得到当前页面所在目录下全名称:request.getServletPath()
    结果:/test/testpath.jsp

    (5)得到页面所在服务器的全路径(实际的路径):application.getRealPath("testpath.jsp")
    结果:D:\Develop Files\apache-tomcat-5.5.15\apache-tomcat-5.5.15\webapps\DemoWeb\testpath.jsp
    D:\Develop Files\apache-tomcat-5.5.15\apache-tomcat-5.5.15为tomcat的安装路径

    2.java 的Class中获得相对路径,绝对路径的方法

    (1)类的绝对路径:
    System.out.println(TestPath.class.getResource("/").getPath());
    结果:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/

    System.out.println(TestPath.class.getResource(""));
    结果:file:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/demo1/


    (2)得到工程的路径:System.getProperty("user.dir")
    结果:E:\workspace\workspace_tcc\DemoWeb

    以下是有关java路径的链接
    Java路径问题总结
    Java相对路径/绝对路径总结(转)

  • 相关阅读:
    安装lamp服务器
    Linux ssh登录命令
    一些替代Xshell的软件推荐
    字符串输入的几种方式
    Java数据库操作的一些注意
    模拟堆
    web安全基础第一天
    情报搜集
    kali中的postgres怎么连接
    LeetCode:Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/549294286/p/3025043.html
Copyright © 2011-2022 走看看