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 ...
随机推荐
- WebKit的已实施srcset图像响应属性
WebKit已经发布了一些官方新闻,终于落实srcset的属性.作为W3C的响应图像社区组的主席,我一直希望这一刻到来有一段时间了.所以,对所有参与方是个好消息,用户浏览网页时的体验是最重要的. 所有 ...
- 学习笔记之LeetCode
LeetCode Online Judge https://leetcode.com/ Leetcode:在线编程网站-各大IT公司的笔试面试题 http://blog.csdn.net/huixin ...
- Linux C 一些函数 所属头文件
1. Linux中一些头文件的作用:<assert.h>:ANSI C.提供断言,assert(表达式)<glib.h>:GCC.GTK,GNOME的基础库,提供很多有用的函数 ...
- sort+函数指针、sort+比较器对象、qsort速度比较
一.上代码 #include<bits/stdc++.h> using namespace std; #define MAXN 50000000 struct TS { int a, b, ...
- MySQL查询结果写入到文件总结
Mysql查询结果导出/输出/写入到文件 方法一:直接执行命令: mysql> select count(1) from table into outfile '/tmp/test.txt'; ...
- Tomcat 实战-调优方案
来自: http://blog.csdn.net/u010028869/article/details/51793821 来自: https://www.cnblogs.com/baihuites ...
- UNITY polygon collider不随物体旋转
U3D中的一般包围框如 boxcollider, meshcollider, capsule collider等都会随物体旋转而旋转.然而polygon collider却不会. 补充:原来所有2D包 ...
- InfoPanel
[InfoPanel] The Info panel shows the color values beneath the pointer and, depending on the tool in ...
- MapReduce Notes
[MapReduce Notes] 1.一个Map/Reduce 作业的输入和输出类型如下所示: 2.Shuffle & Sort & Secondary Sort Reducer的输 ...
- 【bzoj1087】互不侵犯King 状态压缩dp
AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=1087 [题解] 用f[i][j][k]表示前i行放了j个棋子且第i行的状态为k的方案数. ...