java web path
1,request.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。所以它应该是一个通用的方法。
再转:
首先,调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,而后在类类型上调用getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就是保证是和类类型同一个加载器加载的。
最后调用了类加载器的getResourceAsStream()方法来加载资源。
java web path的更多相关文章
- eclipse中web工程新建jsp文件报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
web工程中新建jsp文件提示:The superclass "javax.servlet.http.HttpServlet" was not found on the Java ...
- maven新建web项目提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
maven新建web项目提示The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bu ...
- 转!java web项目 build path 导入jar包,tomcat启动报错 找不到该类
在eclipse集成tomcat开发java web项目时,引入的外部jar包,编译通过,但启动tomcat运行web时提示找不到jar包内的类,需要作如下配置,将jar包在部署到集成的tomcat环 ...
- Eclipse中新建Maven Web项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
在maven web项目中的index.jsp中的错误信息如下: The superclass "javax.servlet.http.HttpServlet" was not f ...
- Java Web报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
问题描述: 我们在用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not foun ...
- java web(jsp)-The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
在静态项目上新建 jsp文件的时候,报错:The superclass "javax.servlet.http.HttpServlet" was not found on the ...
- Maven 梳理 - maven新建web项目提示"javax.servlet.http.HttpServlet" was not found on the Java Build Path
方法一: <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api&l ...
- 高效 Java Web 开发框架 JessMA v3.5.1
JessMA 是功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hibernate.MyBatis 与 J ...
- 高效 Java Web 开发框架 JessMA v3.4.1
JessMA 是功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hibernate.MyBatis 与 J ...
随机推荐
- P1099 树网的核
NOIP 2007 提高第四题. 啊......我还是看了题解才做出来的. 这题乍一看毫无头绪,但是我们spy on一下,暗中观察发现:n才300!随便打暴力水过去啊! 然后,这破题怎么暴力?感觉我的 ...
- mysql常见问题解决
日常使用mysql数据库遇到的一些问题,做下记录,会持续更新. 一.MySql Host is blocked because of many connection errors; unblock w ...
- springboot集成druid连接池
使用druid连接池主要有几步: 1.添加jar和依赖 <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...
- (string高精度)A + B Problem II hdu1002
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 谨记:new Date()在IOS中的坑
第一次遇到,记录一下 要创建一个指定时间的new Date对象时,通常的做法是: new Date("2018-04-27 11:11");然后就可以根据这个获取年月日等信息 这种 ...
- SQL Server 2012/2016/2017 新增函数
/************************************************************** SQL Server 2012 新增的函数 ************** ...
- POJ1742Coins(多重背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32309 Accepted: 10986 Descripti ...
- 自己的Promise
废话不多说,直接上代码: class Promise2{ constructor(fn){ const _this=this; //重点 this.__queue=[]; this.__succ_re ...
- GBDT用于分类问题
一.简介 GBDT在传统机器学习算法里面是对真实分布拟合的最好的几种算法之一,在前几年深度学习还没有大行其道之前,gbdt在各种竞赛是大放异彩.原因大概有几个 一:效果确实挺不错. 二:既可以用于分类 ...
- 面向对象【day08】:静态方法、类方法、属性方法(九)
本节内容 概述 静态方法 类方法 属性方法 总结 一.概述 前面我们已经讲解了关于类的很多东西,今天讲讲类的另外的特性:静态方法(staticmethod).类方法(classmethod).属性方法 ...