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。所以它应该是一个通用的方法。

再转:

this.getClass().getClassLoader().getResource("template"); 
   首先,调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,而后在类类型上调用getClassLoader()方法是得到当前类型的类加载器,我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,而且类加载器之间存在父子关系,就是子知道父,父不知道子,这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),所以在这里通过当前类的加载器来加载资源也就是保证是和类类型同一个加载器加载的。 
最后调用了类加载器的getResourceAsStream()方法来加载资源。

java web path的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 转!java web项目 build path 导入jar包,tomcat启动报错 找不到该类

    在eclipse集成tomcat开发java web项目时,引入的外部jar包,编译通过,但启动tomcat运行web时提示找不到jar包内的类,需要作如下配置,将jar包在部署到集成的tomcat环 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Maven 梳理 - maven新建web项目提示"javax.servlet.http.HttpServlet" was not found on the Java Build Path

    方法一: <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api&l ...

  8. 高效 Java Web 开发框架 JessMA v3.5.1

    JessMA 是功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hibernate.MyBatis 与 J ...

  9. 高效 Java Web 开发框架 JessMA v3.4.1

    JessMA 是功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 Hibernate.MyBatis 与 J ...

随机推荐

  1. bzoj4361 isn (dp+树状数组+容斥)

    我们先设f[i][j]表示长度为i,以j结尾的不降子序列个数,$f[i][j]=\sum{f[i-1][k]},A[k]<=A[j],k<j$,用树状数组优化一下可以$O(n^2logn) ...

  2. bzoj4518/luogu4072 征途(斜率优化dp)

    首先推一波公式: 设f[t][i]为第t天以i为结尾,这时已经算了的最小公差$*m^2$ 设s[i]为1到i的和 $$f[t][i]=min\{f[t-1][j]+m*(s[i]-s[j]-\frac ...

  3. Azure登陆的两种常见方式(user 和 service principal登陆)

    通过Powershell 登陆Azure(Azure MoonCake为例)一般常见的有两种方式 1. 用户交互式登陆 前提条件:有一个AAD account 此种登陆方式会弹出一个登陆框,让你输入一 ...

  4. A1038. Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  5. python(五)——运算符

    1.成员运算符,判断某个东西是否在某个东西里包含:in,not in name = "abcd" if "ac" in name: print("ok ...

  6. react中跨域请求天气预报接口数据

    背景故事:同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能, 如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上 ...

  7. ping的作用

    Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中Ping 是一个十分好用的TCP/IP工具.它主要的功能是用来检测网络的连通情况和分析网络速度. Ping有好的善的一面也有恶的一面.先说一 ...

  8. multidownloadXkcd 多线程抓图

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  9. 在 golang 中使用 Json

    序列化 序列化对象将使用 encoding/json 中的 Marshal 函数. 函数原型为:func Marshal(v interface{}) ([]byte, error) 以下是官网给出的 ...

  10. IoC之Spring.Net在Mvc项目中的使用

    MVC中使用Spring.net 前面学习了使用Autofac来实现控制反转,这里简单记录Spring.Net实现IoC和DI的步骤 第一步:安装如下Nuget包 (Spring.Web.Mvc) i ...