java中获取各种上下文路径的方法小结

前言:

一、context可以理解成一个聚宝盆,在当前环境下你能拿到的参数都可以从context出发去拿,而不仅仅是放URL的.
从里面可以拿到request,session,response.......,可以说只要你拿到了context就可以访问任何你有权限访问的东西
二、context就是“容器”,放的就是应用程序的所有资源,要用时候就访问它,所以context里面的东西,在同一个应用程序里面是全局的。
三、是一个包含各种context的设置的对象
例如:pageContext就包含了该页面的各种设置。可以通过他得到该页面所在服务器路径等等。
Context 用于在 Request 操作中指定上下文对象的对象,上下文字符串在与请求调用一起被发送之前,
必须在该指定的上下文对象中进行解析。Context 对象包含 NamedValue 对象形式的属性列表。这些属性表示关于客户端、
环境或请求情形的信息,它们通常是一些可能不方便作为参数传递的属性。

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

在servlet里用:
this.getServletContext().getRealPath();

在struts用:
this.getServlet().getServletContext().getRealPath();

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

以上三个获得都是当前运行文件在服务器上的绝对路径
其实:
request.getRealPath("url"); // 虚拟目录映射为实际目录
request.getRealPath() 这个方法已经不推荐使用了,代替方法是:
request.getSession().getServletContext().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("/");

以工程名为news为例: http://localhost:8080/news/main/list.jsp

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

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

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

(4) 得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/news/main/list.jsp

(5) 得到相对地址:
request.getRequestURI()

结果:/news/main/list.jsp  

(6) 得到页面所在服务器的全路径
application.getRealPath("页面.jsp")

结果:D:\resin\webapps\news\main\list.jsp

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

三、在类中取得路径

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

例如:

String path=this.getClass().getResource("/").getPath();//得到d:/tomcat/webapps/工程名WEB-INF/classes/路径  
       path=path.substring(1, path.indexOf("WEB-INF/classes"));//从路径字符串中取出工程路径

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

结果:D:\TEST

四、在Servlet中取得路径
(1) 得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\news

复制代码
import java.io.File;
public class Test {
   public static void main(String[] args) throws Exception {
      System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
      System.out.println(Test.class.getClassLoader().getResource(""));
      System.out.println(ClassLoader.getSystemResource(""));
      System.out.println(Test.class.getResource(""));
      System.out.println(Test.class.getResource("/")); //Class文件所在路径
      System.out.println(new File("/").getAbsolutePath());
      System.out.println(System.getProperty("user.dir"));
   }
}
复制代码

五、系统路径说明: 

System.getProperty("user.dir")

如果是.bat或者.sh文件的话,那么该语句输出的时候.bat或者.sh所在的文件路径;

如果是weblogic服务器且由类输出的话,该语句输出的路径是./startWebLogic.sh文件所在的路径

        String osName = System.getProperties().getProperty("os.name");  
        if(osName.equals("Linux")){  
            System.setProperty("CPATH",System.getProperty("user.dir")+"/cssconfig.properties");
        }else{  
            System.setProperty("CPATH",System.getProperty("user.dir")+"\\cssconfig.properties");
        }

根据操作系统设定不同路径。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值