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 ...
随机推荐
- bypy 用法
因为服务器上爬取到的40个G的数据采用http,ftp,scp传输到本地效果不太理想.找资料就看到了bypy,利用百度云存储解决这个问题,解决了服务器带宽有限的问题. pip install bypy ...
- android + eclipse + 后台静默安装(一看就会)
首先要说到三个类. import android.content.pm.IPackageInstallObserver; import android.content.pm.IPackageIns ...
- java:给图片上加水印
import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buff ...
- pascal与其它语言代码书写的不同和pascal的快捷键
1.把很多简单语句括起来 begin end; 2.开始,结束 begin end. 3.判断if if (布尔表达式) then begin 语句 end //没有‘:’ else if () b ...
- opencv 霍夫变换 实现图片旋转角度计算
在OCR实际开发中,证件照采集角度有很大的偏差,需要将图片进行旋转校正, 效果图: 在应用中发现应该加入高斯模糊,可以极大减少误差线条. 知道线条后 通过求斜率 得旋转角度 .(x1-x2)/(y1- ...
- Linux记录-进程数和句柄数调整
1.cat /etc/security/limits.confwebuser soft nofile 65535webuser hard nofile 65535webuser soft nproc ...
- .NET MVC中的防CSRF攻击
一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSR ...
- 一个Entity Framework、ADO.NET查询性能测试
Entity Framework自然是会比ADO.NET性能慢点,这个不多说了.直接上结果. 本该用测试项目的,不过我建了个aspx.下面是随便测20遍得到的结果 补充!!把12行改成 list = ...
- OpenCV在字符提取中进行的预处理(转)
OCR简介熟悉OCR的人都了解,OCR大致分为两个部分: -文字提取text extractor -文字识别text recognition 其中,第一部分是属于图像处理部分,涉及到图像分割的知识,而 ...
- mysql select 时间戳转标准时间写法
select FROM_UNIXTIME(create_time, '%Y-%m-%d %H:%i:%S') as create_time from tabName