zoukankan      html  css  js  c++  java
  • Java Web获取Web应用根路径

    最基本的

    1,request.getRealPath("/");这个方法已不推荐用 --> request.getSession().getServletContext().getRealPath("/")

     

    2,在Servlet 里用this.getServletContext().getRealPath("/");获得要路径。
          或者 request.getSession().getServletContext().getRealPath("/")来获取。

     

    3,struts里用this.getServlet().getServletContext().getRealPath("/")获得要路径。

    在spring中,可以通过

        ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/")  来获取。

     

     

    再转java获取WEB服务真实路径 :

    (1)、request.getRealPath("/");//不推荐使用获取工程的根路径
    (2)、request.getRealPath(request.getRequestURI());//获取jsp的路径,这个方法比较好用,可以直接在servlet和jsp中使用
    (3)、request.getSession().getServletContext().getRealPath("/");//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用
    (4)、this.getClass().getClassLoader().getResource("").getPath();//获取工程 classes下的路径,这个方法可以在任意jsp,servlet,java文件中使用,因为不管是jsp,servlet其实都是java程序,都是 一个class。所以它应该是一个通用的方法。

      

     
    request 获取几种路径
    System.out.println(request.getRequestURI()); // /ms/baseActionForUrl.action
    System.out.println(request.getRequestURL()); // http://127.0.0.1:8080/ms/baseActionForUrl.action
    System.out.println(request.getContextPath()); // /ms
    System.out.println(request.getServletPath()); // /baseActionForUrl.action
     
  • 相关阅读:
    利用SVN进行个人代码管理
    ECEF坐标系
    地理坐标系、大地坐标系、投影坐标系
    让VS能够识别我的DLL运行库
    cannot convert parameter 1 from 'const char *' to 'LPCWSTR' 修改
    创建文件目录C++ windows
    GDAL获取遥感图像基本信息
    全球经纬度划分
    遥感影像度与米的转换
    C++ assert用法
  • 原文地址:https://www.cnblogs.com/hzm112567/p/2936308.html
Copyright © 2011-2022 走看看