java路径问题
来源:互联网 发布:三了个七淘宝 编辑:程序博客网 时间:2024/06/15 14:21
equest.getRealPath() 这个方法已经不推荐使用了,代替方法是:
request.getSession().getServletContext().getRealPath()
从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各个对应方法所代表的含义
参考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获取各种路径总结
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.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
阅读全文
0 0
- JAVA中的路径问题
- java中路径问题
- java 路径问题
- Java中的路径问题
- java路径问题
- java路径问题总结
- java中路径问题
- java 中 路径问题
- Java路径问题
- Java 文件路径问题
- Java的路径问题
- 关于java路径问题
- java action 路径问题
- java 路径问题2
- java 路径问题3
- java路径问题
- Java的路径问题
- Java路径问题讨论
- 使用Spring、SpringMvc、MyBatis搭建SSM框架
- Angular 4_监听页面大小变化
- 虚拟机监控与故障处理工具
- odex修改,解决 android 5.0 以下 uiautomator 遇到特殊字符和emoji表情失败崩溃的错误
- Git使用步骤
- java路径问题
- js伪数组转成数组
- JAVAWEB第三天案例代码
- 如何解决滚动条使页面跳动的问题
- 我的常用命令语句
- 单行显示文字, 超出部分显示省...
- mosh和screen的操作
- CentOS7网卡无法启动的解决方法
- 歪果仁是如何过黑五的