从request获取各种路径总结 转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839 equest.getRealPath()
https://www.cnblogs.com/hello-yao-ge/p/6606564.html
转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839
equest.getRealPath() 这个方法已经不推荐使用了,代替方法是:
request.getSession().getServletContext().getRealPath()
![](/assets/blank.gif)
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String remoteAddress=request.getRemoteAddr(); String servletPath=request.getServletPath(); String realPath=request.getRealPath("/"); String remoteUser=request.getRemoteUser(); String requestURI=request.getRequestURI(); out.println("path:"+path+"<br>"); out.println("basePath:"+basePath+"<br>"); out.println("remoteAddr:"+remoteAddress+"<br>"); out.println("servletPath:"+servletPath+"<br>"); out.println("realPath:"+realPath+"<br>"); out.println("remoteUser:"+remoteUser+"<br>"); out.println("requestURI:"+requestURI+"<br>"); 结果: path:/WebDemo basePath:http://localhost:8683/WebDemo/ remoteAddr:127.0.0.1 servletPath:/index.jsp realPath:D:\apache-tomcat-6.0.13\webapps\WebDemo\ remoteUser:null requestURI:/WebDemo/index.jsp 从上不难看出request各个对应方法所代表的含义
![](/assets/blank.gif)
参考servlet中的接口:
request.getScheme();
返回的协议名称,默认是http
request.getServerName()
返回的是你浏览器中显示的主机名,你自己试一下就知道了
getServerPort()
获取服务器端口号
例如:
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/test/*</url-pattern>
</servlet-mapping>
dwr为工程名
http://localhost:8080/dwr/servlet/test/joejoe1991/a.html?name=test
getPathInfo() 返回的仍然是:
"/joejoe1991/a.html" ,而并不包括后面的"?name=test";
在servlet里用this.getServletContect().getRealPath()
在struts里用this.getServlet().getServletContext().getRealPath()
在Action里用ServletActionContext.getRequest().getRealPath();
以上三个获得都是当前运行文件在服务器上的绝对路径
request.getRealPath("url"); // 虚拟目录映射为实际目录
request.getRealPath("./"); // 网页所在的目录
request.getRealPath("../"); // 网页所在目录的上一层目录
request.getContextPath(); // 应用的web目录的名称
如http://localhost:7001/bookStore/
/bookStore/ => [contextPath] (request.getContextPath())
获取Web项目的全路径
String strDirPath = request.getSession().getServletContext().getRealPath("/");
以工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST
2.在类中取得路径:
(1)类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/
(2)得到工程的路径:System.getProperty("user.dir")
结果:D:\TEST
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
从request获取各种路径总结 转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839 equest.getRealPath()相关推荐
- Win10双击调试 转载 http://blog.csdn.net/sagittarius_warrior/article/details/51305046
转:注1:本文非本人所写,尊重原作者劳动成果.仅是对原文备份. http://blog.csdn.net/sagittarius_warrior/article/details/51305046(原来 ...
- (转)PC机使用网络调试助手获取onenet的数据(https://blog.csdn.net/qq_29219435/article/details/78480872)
在上一篇博客中,我写了如何向onenet发送数据,本文主要介绍通过http协议GET到之前上传的数据. 1 2 http://blog.csdn.net/qq_29219435/article/det ...
- 转载 http://blog.csdn.net/dengta_snowwhite/article/details/6418384
从SDCard保存的txt文件读取中文到android系统中会出现乱码问题,如何解决这个乱码问题,网上有不少解答方法,譬如说利用String temp1 =EncodingUtils.getStrin ...
- 转载(https://blog.csdn.net/qq_36738482/article/details/72823509)大数据的概念
1.大数据定义 对于"大数据"(Big data)研究机构Gartner给出了定义,"大数据"是需要新处理模式才能具有更强的决策力.洞察发现力和流程优化能力 的 ...
- 手机网络抓包 转载记录http://blog.csdn.net/skylin19840101/article/details/43485911
Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求.Fiddler能捕获IOS设备发出的请求,比如IPhone, IPad, MacBook. ...
- Android Volley完全解析(二),使用Volley加载网络图片 转载:http://blog.csdn.net/guolin_blog/article/details/174
转载:http://blog.csdn.net/guolin_blog/article/details/17482165 在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中 ...
- Android Volley完全解析(一),初识Volley的基本用法 转载地址:http://blog.csdn.net/guolin_blog/article/details/17482095
转载地址:http://blog.csdn.net/guolin_blog/article/details/17482095 1. Volley简介 我们平时在开发Android应用的时候不可避免地都 ...
- WordPress数据库的基本构造(转载自http://blog.csdn.net/ppiao1970hank/article/details/6301812)...
转载自http://blog.csdn.net/ppiao1970hank/article/details/6301812 WordPress使用MySQL数据库.作为一个开发者,我们有必要掌握Wor ...
- Ubuntu安装软件是始终出现dpkg错误(转载,原文链接:https://blog.csdn.net/cyf15238622067/article/details/53534629)
Ubuntu安装软件是始终出现dpkg错误 一.设置镜像 二. 出现错误死循环时,使用下述命令 sudo dpkg -i --force-overwrite sudo dpkg -i --force- ...
最新文章
- go 向buff写入一个字节_我在 Go 中犯的 5 个错误
- ES6系列--对象扩展
- 《IBM-PC汇编语言程序设计》(第2版)【沈美明 温冬婵】——自编解析与答案
- ansible 修改文件变量_Ansible Playbook中的变量与引用
- 计算机四级数据库真题,2015年计算机四级数据库真题
- 函数 strncpy、strncat、strncmp、strrchr 的实现
- PCL编程笔记——Assertion `px != 0' failed.
- SET NOCOUNT
- 3dmax渲染器下载VRay4.1渲染器下载安装教程(支持3dmax2013-2019)
- php网页动态加载swf,HTML_如何修改网页中的FlashSWF文件,看到一个网站用flash做的,很 - phpStudy...
- HTML5桌面应用程序开发
- 为什么“蜥蜴书”是机器学习入门必读书?
- Linux UDP协议栈中的片段分析 - udp_recvmsg
- 细节复盘3 (文本折叠、文本尾行缩进截断)2020-8-2
- 世界上服务器最多的游戏,魔兽科普 国服人最多的几个服务器都什么来头
- 正则表达式 基本汉字 特殊字符
- scada与MySQL连接_SCADA系统与实时数据库数据同步
- 电大计算机科学与技术专业论文,国家开放大学计算机科学与技术专业毕业设计(论文)要求...
- 详细解读什么是自适应巡航?
- 基于PHP学生成绩查询系统设计与实现 开题报告