zoukankan      html  css  js  c++  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 Filesapache-tomcat-5.5.15apache-tomcat-5.5.15webappsDemoWeb estpath.jsp 
    D:Develop Filesapache-tomcat-5.5.15apache-tomcat-5.5.15为tomcat的安装路径 

    (6) Web应用中有各种获取path或URI,URL的方法,假设网页访问地址:

    http://localhost:8080/tradeload/TestServlet

    Web应用context: /tradeload 

    1. request.getContextPath()= /tradeload   
    2. request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()= http://localhost:8080   
    3. request.getRequestURL() = http://localhost:8080/tradeload/TestServlet   
    4. request.getRequestURI() = /tradeload/TestServlet   
    5. request.getPathInfo() = null   
    6. request.getServletPath() = /TestServlet   
    7. getServletContext().getRealPath('/') = C:serverglassfishdomainsdomain1applicationsj2ee-modules radeload

    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:workspaceworkspace_tccDemoWeb 

    (3)得到项目部署的绝对路径:

    //获取运行项目名称
    WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
    ServletContext sc =wac.getServletContext();
    String projectName = sc.getContextPath();

    //获取运行项目路径 即tomcat下的项目路径(默认情况下)
    //tomcat conf server.xml配置文件若有配置项目路径<Context path="" docBase="D:seed" reloadable="false" />,则是该docBase的路径
    WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
    ServletContext servletContext = webApplicationContext.getServletContext();
    String projectPath=servletContext.getRealPath("/").replace("\", "/");

  • 相关阅读:
    Windows 下安装 Python + Django
    asp.net core 一个中小型项目实战的起手式——Swagger配置
    asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)
    c#一些常用知识点
    ADO.NET中的5个主要对象
    JavaScript的闭包特性如何给循环中的对象添加事件(一)
    在什么情况下会用到虚方法?它与接口有什么不同?
    一般处理程序Session
    什么是code-Behind技术?
    什么是事务?
  • 原文地址:https://www.cnblogs.com/tyxa/p/5813393.html
Copyright © 2011-2022 走看看