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

    从request获取各种路径总结 

    httpServletRequest.getRequestURI()== getContextPath() + getServletPath() + getPathInfo()
    request.getRealPath("url"); // 虚拟目录映射为实际目录


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

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

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

    request.getServletPath();  /   /得到当前页面所在目录下全名称

    request.getPathInfo();          //这个方法返回请求的实际URL相对于请求的serlvet的url的路径

    假定你的工程名称为projects,你在浏览器中输入请求路径:

    http://127.0.0.1:8080/projects/pages/newForm.jsp

    则执行下面向行代码后打印出如下结果:
    1、 System.out.println(request.getContextPath());
    打印结果:/projects
     2、System.out.println(request.getServletPath());
    打印结果:/pages/newForm.jsp
     3、 System.out.println(request.getRequestURI());
    打印结果:/projects/pages/newForm.jsp
     4、 System.out.println(request.getRealPath("/")); 
     JSP servlet API提供了getRealPath(path)方法,返回给定虚拟路径的真实路径,如果转换错误,则返回null。

    打印结果:C:Tomcat5.0webappsprojects est


    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

  • 相关阅读:
    Python深入03 对象的属性
    利用Webkit抓取动态网页和链接
    分享:OCILIB 3.11.0 发布,跨平台 Oracle 驱动
    Knockoutjs实战开发:控制子绑定(control descendant bindings)
    利用InjectedBundle定制自己的Webkit(二)
    使用solrj和EasyNet.Solr进行原子更新
    Chaos网络库(二) Buffer的设计
    分享:djangohaystack+solr实现搜索
    Moon.ORM 4.4 隆重发布,在性能和使用便捷上一挑群雄(mysoft,cyq,pdf)
    数据结构利器之私房STL(中)
  • 原文地址:https://www.cnblogs.com/coderL/p/7543812.html
Copyright © 2011-2022 走看看