zoukankan      html  css  js  c++  java
  • request获取各种路径

    equest.getRealPath() 这个方法已经不推荐使用了,代替方法是:

    request.getSession().getServletContext().getRealPath()

    在servlet里用this.getServletContect().getRealPath()

    在struts里用this.getServlet().getServletContext().getRealPath()

    在Action里用ServletActionContext.getRequest().getRealPath();

    以上三个获得都是当前运行文件在服务器上的绝对路径

     
     
    从request获取各种路径总结 
    request.getRealPath("url"); // 虚拟目录映射为实际目录 


    request.getRealPath("./");    // 网页所在的目录 

    request.getRealPath("../"); // 网页所在目录的上一层目录 

    request.getContextPath();    // 应用的web目录的名称 


    如http://localhost:7001/bookStore/ 
    /bookStore/ => [contextPath] (request.getContextPath()) 

    获取Web项目的全路径 
    String strDirPath = request.getSession().getServletContext().getRealPath("/"); 

    以工程名为TEST为例: 

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


    (2)得到工程名:request.getContextPath() 
    结果:/TEST 


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


    (4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp") 
    结果:D: esinwebappsTEST est.jsp 


    (5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 
    结果:D: esinwebappsTEST 

    2.在类中取得路径: 

    (1)类的绝对路径:Class.class.getClass().getResource("/").getPath() 
    结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ 


    (2)得到工程的路径:System.getProperty("user.dir") 
    结果:D:TEST 

    3.在Servlet中取得路径: 

    (1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。 
    结果:E:TomcatwebappsTEST 


    (2)得到IE地址栏地址:request.getRequestURL() 
    结果:http://localhost:8080/TEST/test 


    (3)得到相对地址:request.getRequestURI() 
    结果:/TEST/test
     
    (4)得到项目的路径:request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()
    结果:http://localhost:8080/TEST
     
     
     
  • 相关阅读:
    poj2623
    poj2635
    案例解析丨 Spark Hive 自定义函数应用
    云图说 | 华为云GPU共享型AI容器,让你用得起,用得好,用的放心
    云小课 |选定合适的证书,做“有证”的合规域名
    记一次 node 项目重构改进
    SpringBoot写后端接口,看这一篇就够了!
    如何让知识图谱告诉你“故障根因”
    我敢说,这个版本的斗地主你肯定没玩过?
    5 分钟带你掌握 Makefile 分析
  • 原文地址:https://www.cnblogs.com/tk55/p/6655349.html
Copyright © 2011-2022 走看看