jsp request 获取路径
这篇教程不错: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 获取路径的更多相关文章
- jsp Request获取url信息的各种方法比较
从Request对象中可以获取各种路径信息,以下例子: 假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下 String p ...
- request获取路径方式
从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...
- request获取路径
1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数. 2.request.getRequestURI( ...
- [转]java中通过request获取路径中的不同信息
原文地址:http://blog.csdn.net/lv_shijun/article/details/40819859 aa为工程中的项目名 bb为webRoot下的文件夹 1.request.ge ...
- jsp中获取路径信息的方法
今天在看代码时,发现程序使用了 request.getScheme() .不明白是什么意思,查了一下.结果整理如下: 1.request.getScheme() 返回当前链接使用的协议:一般应用返回h ...
- request获取各种路径总结、页面跳转总结。
页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...
- Java获取路径的方法分析详解(Application/Web)
1.利用System.getProperty()函数获取当前路径: System.getProperty("user.dir");//user.dir用户当前的工作目录,输出:D: ...
- request 获取各种路径
从request获取各种路径总结 request.getRealPath("url"); // 虚拟目录映射为实际目录 request.getRealPath("./&q ...
- request获取各种路径
equest.getRealPath() 这个方法已经不推荐使用了,代替方法是: request.getSession().getServletContext().getRealPath() 在ser ...
随机推荐
- Oracle归档日志与非归档日志的切换及路径设置
--==================== -- Oracle 归档日志 --==================== Oracle可以将联机日志文件保存到多个不同的位置,将联机日志转换为归档日志的 ...
- linux下启动springboot服务
错误日志 SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] . __ ...
- 关于FastCgi与PHP-fpm之间是个什么样的关系【转自知乎】
刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少. 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...
- SQL Server数据库partition by 与ROW_NUMBER()函数使用详解[转]
关于SQL的partition by 字段的一些用法心得 先看例子: if object_id('TESTDB') is not null drop table TESTDB create table ...
- gridview发布后,编辑改为edit 原因是未安装 dotNetFx40LP_Full_x86_x64zh-Hans中文语言包
https://www.microsoft.com/zh-cn/download/details.aspx?id=3324
- vc通过进程名返回进程id
std::string WcharToChar(const wchar_t* wp, size_t m_encode = CP_ACP) { std::string str; , wp, wcslen ...
- android处理Back键Home键和Menu键事件(转)
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BAC ...
- 迷你MVVM框架 avalonjs 学习教程6、插入移除处理
ms-if是属于流程绑定的一种,如果表达式为真值那么就将当前元素输出页面,不是就将它移出DOM树.它的效果与上一章节的ms-visible效果看起来相似的,但它会影响到:empty伪类,并能更节约性能 ...
- VS Code 界面语言设置
首先Ctrl+shift+p打开命令面板. 输入配置显示语言 回车 更改locale即可更改页面显语言(更改后需重启软件). 目前支持如下语言 Display Language Locale Engl ...
- NBU 还原windows ORACLE数据库(BW)
将0.92数据库还原到1.92,还原时间7月13日 1.查询所需要的控制日志 bplist -C bw-prd01 -s -- -e -- -k oracle_bw-prd01_0_92_arch - ...