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. 【bzoj3064】 CPU监控

    http://www.lydsy.com/JudgeOnline/problem.php?id=3064 (题目链接) 题意 给出一个长度为$n$的数列$A$,同时定义一个辅助数组$B$,$B$开始与 ...

  2. A1005. Spell It Right

    Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...

  3. (转)synchronized和lock的区别

    背景:最近在准备java基础知识,对于可重入锁一直没有个清晰的认识,有必要对这块知识进行总结. 1 . 什么是可重入锁 锁的概念就不用多解释了,当某个线程A已经持有了一个锁,当线程B尝试进入被这个锁保 ...

  4. VUE在BODY上绑定enter事件

    mounted () { this.bodyListener = (e) => { if (e.keyCode === 13 && e.target === document.b ...

  5. 洛谷 P1880 [NOI1995] 石子合并(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题解: 这道题是石子合并问题稍微升级版 这道题和经典石子合并问题的不同在于,经典的石子合 ...

  6. php redis在windows下的部署

    1.下载php扩展 下载地址:https://github.com/phpredis/phpredis/downloads 2.将下载php扩展redis放到php的ext目录下,然后在php.ini ...

  7. go语言从零学起(四) -- 基于martini和gorilla实现的websocket聊天实例

    如果只是想了解chat的实现方式,在gorilla和revel框架里面都有完整的chat实例可以提供参考.本篇讲解的是,如何基于martini实现websocket的聊天. 配置步骤: 1 已经安装了 ...

  8. curl模拟http发送get或post接口测试

    一.get请求 curl "http://www.baidu.com"  如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...

  9. JSTL中forEach标签应用示例【转】【补】

    forEach样例 <%@ page language="java" import="java.util.*" pageEncoding="ut ...

  10. CentOS6.8搭建rabbitmq消息中间件

    参考资料:http://blog.csdn.net/yunfeng482/article/details/72853983 一.rabbitmq简介 MQ全称为Message Queue, 消息队列( ...