这篇教程不错:http://zjutsoft.iteye.com/blog/1084260

自己试验如下:

System.out.println("-----------------servlet-----------------");
System.out.println("realpath null:"+request.getRealPath(""));
System.out.println("realpath index.jsp:"+request.getRealPath("index.jsp"));
System.out.println("realpath ./:"+request.getRealPath("./"));
System.out.println("realpath ../:"+request.getRealPath("../"));
System.out.println("contextpath:"+request.getContextPath());
System.out.println("web项目的全路径:"+request.getSession().getServletContext().getRealPath("/"));
System.out.println("URI:"+request.getRequestURI());
System.out.println("URL:"+request.getRequestURL());
System.out.println("servletpath:"+request.getServletPath());
//System.out.println("页面在服务器的绝对路径:"+new java.io.File(application.getRealPath(request.getRequestURI())).getParent()); System.out.println("~~~~~~~~~~~~~~~页面~~~~~~~~~~~~~~~~~");
System.out.println("realpath null:"+request.getRealPath(""));
System.out.println("realpath index.jsp:"+request.getRealPath("index.jsp"));
System.out.println("realpath ./:"+request.getRealPath("./"));
System.out.println("realpath ../:"+request.getRealPath("../"));
System.out.println("contextpath:"+request.getContextPath());
System.out.println("web项目的全路径:"+request.getSession().getServletContext().getRealPath("/"));
System.out.println("URI:"+request.getRequestURI());
System.out.println("URL:"+request.getRequestURL());
System.out.println("servletpath:"+request.getServletPath());
System.out.println("页面在服务器的绝对路径:"+new java.io.File(application.getRealPath(request.getRequestURI())).getParent()); //结果
-----------------servlet-----------------
realpath null:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt
realpath index.jsp:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\index.jsp
realpath ./:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\.
realpath ../:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\..
contextpath:/jyxt
web项目的全路径:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\
URI:/jyxt/hello
URL:http://localhost:8080/jyxt/hello
servletpath:/hello
~~~~~~~~~~~~~~~页面~~~~~~~~~~~~~~~~~
realpath null:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt
realpath index.jsp:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\index.jsp
realpath ./:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\.
realpath ../:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\..
contextpath:/jyxt
web项目的全路径:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\
URI:/jyxt/WEB-INF/view/index.jsp
URL:http://localhost:8080/jyxt/WEB-INF/view/index.jsp
servletpath:/WEB-INF/view/index.jsp
页面在服务器的绝对路径:D:\eclipse-jee-luna-SR1-win32\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\jyxt\jyxt\WEB-INF\view

在类中获取路径先不看,其他的只有10种方法。

两种应用场景:.jsp和servlet中。jsp中realPath被抛弃,不过返回值跟servlet中的一样,getRealPath("./   ../")返回值都不太对;页面在服务器的绝对路径 就不能用了。

getContextPath() 和 .getSession().getServletContext().getRealPath("/") 在.jsp和servlet中返回值都一样;URI,URL,servletpath都不一样。

//http
System.out.println("scheme:"+request.getScheme());
//localhost
System.out.println("name:"+request.getServerName());
//8080
System.out.println("port:"+request.getServerPort());

常用方法

在servlet中:getContextPath(), URI, URL, servletPath(),
在jsp中:getContextPath()

jsp request 获取路径的更多相关文章

  1. jsp Request获取url信息的各种方法比较

    从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...

  2. request获取路径方式

    从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...

  3. request获取路径

    1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数. 2.request.getRequestURI( ...

  4. [转]java中通过request获取路径中的不同信息

    原文地址:http://blog.csdn.net/lv_shijun/article/details/40819859 aa为工程中的项目名 bb为webRoot下的文件夹 1.request.ge ...

  5. jsp中获取路径信息的方法

    今天在看代码时,发现程序使用了 request.getScheme() .不明白是什么意思,查了一下.结果整理如下: 1.request.getScheme() 返回当前链接使用的协议:一般应用返回h ...

  6. request获取各种路径总结、页面跳转总结。

    页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...

  7. Java获取路径的方法分析详解(Application/Web)

    1.利用System.getProperty()函数获取当前路径: System.getProperty("user.dir");//user.dir用户当前的工作目录,输出:D: ...

  8. request 获取各种路径

    从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...

  9. request获取各种路径

    equest.getRealPath() 这个方法已经不推荐使用了,代替方法是: request.getSession().getServletContext().getRealPath() 在ser ...

随机推荐

  1. wamp ssl配置https

    主要参考的这个博客 http://blog.csdn.net/sdq4700/article/details/36173665 配置完以后重启wamp , https不能正常访问 apache.log ...

  2. WIN10下搭建react-native开发Android环境

    最近公司要求使用react-native进行移动端开发,据说macOS上开发坑会少的多,但我们是windows,莫法,直接抗吧!周末配置环境遇到很多问题,谨以此文做个记录... 准备 安装Chocol ...

  3. Linux服务器安全之用户密钥认证登录

    转自:http://blog.sina.com.cn/s/blog_6561ca8c0102vb0d.html 一. 密钥简介 在Linux下,远程登录系统有两种认证方式:密码认证和密钥认证.密码认证 ...

  4. CentOS下MySQL的彻底卸载

      #################CentOS7下MySQL的卸载#################### 1:查看MySQL是否安装: 方式1: [root@localhost usr]# yu ...

  5. IO在block级别的过程分析

    btt User Guide在百度找了3天没找到,bing也不行,结果google第一页第5个结果就是. 可恶的GFW http://www.fis.unipr.it/doc/blktrace-1.0 ...

  6. django-DIL模板自定义过滤器,自定义标签,自定义包含标签

    自定义过滤器 DTL模板语言生来只是为了方便的展示信息,所以与编程语言相比显得有点薄弱,有时候不能满足我们的需求.因此django提供了一个接口,让开发者能自定义标签和过滤器. 首先,你需要添加一个t ...

  7. vlc框架流程解析(转)

    原文地址:http://luzefengoo.blog.163.com/blog/static/1403593882012754481846/ 第二部分 程序框架实现 1. 播放列表文件src/pla ...

  8. 为何在JDK安装路径下存在两个JRE?

    "两个jre"和"三个lib"的功能简单扼要的解释 安装JDK后,Java目录下有jdk和jre两个文件夹,但jdk下还有一个jre文件夹,而且这个jre比前面 ...

  9. springboot+shiro+jwt

    1.添加依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-sp ...

  10. Linux下MariaDB 安装及root密码设置(修改)

    根据官方说明在/etc/yum.repo.d/下添加repo: # MariaDB 10.2 Fedora repository list - created 2017-11-25 05:55 UTC ...