JavaWeb项目中各种路径的获取
以工程名为/DemoWeb为例:
访问的jsp为: http://localhost:8080/DemoWeb/test/index.jsp
1 JSP中获得当前应用的相对路径和绝对路径
(1)得到工程名:request.getContextPath()
结果:/DemoWeb
(2)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/DemoWeb/test/testpath.jsp
(3)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/DemoWeb/test/testpath.jsp
(4)得到当前页面所在目录下全名称:request.getServletPath()
结果:/test/testpath.jsp
(5)得到页面所在服务器的全路径(实际的路径):application.getRealPath("testpath.jsp")
结果:D:\Develop Files\apache-tomcat-5.5.15\apache-tomcat-5.5.15\webapps\DemoWeb\testpath.jsp
D:\Develop Files\apache-tomcat-5.5.15\apache-tomcat-5.5.15为tomcat的安装路径
(6) Web应用中有各种获取path或URI,URL的方法,假设网页访问地址:
http://localhost:8080/tradeload/TestServlet
Web应用context: /tradeload
request.getContextPath()= /tradeload
request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()= http://localhost:8080
request.getRequestURL() = http://localhost:8080/tradeload/TestServlet
request.getRequestURI() = /tradeload/TestServlet
request.getPathInfo() = null
request.getServletPath() = /TestServlet
getServletContext().getRealPath('/') = C:\server\glassfish\domains\domain1\applications\j2ee-modules\tradeload\
2.java 的Class中获得相对路径,绝对路径的方法
(1)类的绝对路径:
System.out.println(TestPath.class.getResource("/").getPath());
结果:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/
System.out.println(TestPath.class.getResource(""));
结果:file:/E:/workspace/workspace_tcc/DemoWeb/WebRoot/WEB-INF/classes/demo1/
(2)得到工程的路径:System.getProperty("user.dir")
结果:E:\workspace\workspace_tcc\DemoWeb
(3)得到项目部署的绝对路径:
//获取运行项目名称
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
ServletContext sc =wac.getServletContext();
String projectName = sc.getContextPath();
//获取运行项目路径 即tomcat下的项目路径(默认情况下)
//tomcat conf server.xml配置文件若有配置项目路径<Context path="" docBase="D:\seed" reloadable="false" />,则是该docBase的路径
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
String projectPath=servletContext.getRealPath("/").replace("\\", "/");
JavaWeb项目中各种路径的获取的更多相关文章
- JAVA WEB项目中各种路径的获取
JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报 分类: JAVA开发(41) 1.可以在s ...
- web项目中各种路径的获取(复制,为以后好找资源)
web项目中各种路径的获取 1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获 ...
- 理解JavaWeb项目中的路径问题——相对路径与绝对路径
背景: 在刚开始学习javaweb,使用servlet和jsp开发web项目的过程中,一直有一个问题困扰着我:servlet 和 jsp 之间相互跳转,跳转的路径应该如何书写,才能正确的访问到相应的s ...
- javaWeb项目中的路径格式 请求url地址 客户端路径 服务端路径 url-pattern 路径 获取资源路径 地址 url
javaweb项目中有很多场景的路径客户端的POST/GET请求,服务器的请求转发,资源获取需要设置路径等这些路径表达的含义都有不同,所以想要更好的书写规范有用的路径代码 需要对路径有一个清晰地认知 ...
- web项目中各种路径的获取
以工程名为/DemoWeb为例: 访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工程 ...
- javaweb项目中绝对路径的写法理解
Tomcat的默认访问路径为http://localhost:8080,后需添加项目路径. 请求转发,是转发到本项目中的其他文件,所以在默认访问路径中添加了本项目的项目路径,故可以省略项目名称: re ...
- web项目中各种路径的获取HttpServletRequest
以工程名为/DemoWeb为例: 访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工程 ...
- JavaWeb 项目中的绝对路径和相对路径以及问题的解决方式
近期在做JavaWeb项目,总是出现各种的路径错误,并且发现不同情况下 / 所代表的含义不同,导致在调试路径上浪费了大量时间. 在JavaWeb项目中尽量使用绝对路径 由于使用绝对路径是绝对不会出 ...
- 关联分析FPGrowth算法在JavaWeb项目中的应用
关联分析(关联挖掘)是指在交易数据.关系数据或其他信息载体中,查找存在于项目集合或对象集合之间的频繁模式.关联.相关性或因果结构.关联分析的一个典型例子是购物篮分析.通过发现顾客放入购物篮中不同商品之 ...
随机推荐
- 剑指 offer set 22 数组中的逆序数
总结 1. 题目为归并排序的变形, 不过我完全没想到 2. 在归并排序进行字符组 merge 时, 统计逆序数. merge 后, 两个子数组是有序的了, 下次再 merge 的时候就能以 o(n) ...
- 高级service之ipc ADIL用法
感谢 如果你还没有看过前面一篇文章,建议先去阅读一下 Android Service完全解析,关于服务你所需知道的一切(上) ,因为本篇文章中涉及到的代码是在上篇文章的基础上进行修改的. 在上篇文章中 ...
- Servlet与JSP九大内置对象的对应关系
JSP对象 Servlet中怎样获得 out resp.getWriter request service方法中的req参数 response service方法中的resp参数 session re ...
- c++11 lambda(了解)
this->send_change_equip = ([this](ChangeEquipPT channge) { send_cmd(s2c_change_equip, &channg ...
- java枚举类型(转载)
public class TestEnum { /*最普通的枚举*/ public enum ColorSelect { red, green, yellow, blu ...
- LeetCode-Lowest Common Ancestor of a Binary Tre
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- linux下安装mysql-5.7.25
1.下载对应安装包 https://dev.mysql.com/downloads/mysql/ 2.卸载旧版本mysql 列出旧版本MySql的组件列表 rpm -qa | grep mysql ...
- Spoken English Practice(1、This is between you and me, Don't let it out. 2、Don't let your dreams be dreams, no matter how hard it gets, say to yourself, I'm going to make it.)
绿色:连读: 红色:略读: 蓝色:浊化: 橙色:弱读 下划线_为浊化 口语蜕变(2017/7/12) ...
- 粘性会话 session affinity sticky session requests from the same client to be passed to the same server in a group of servers
Module ngx_http_upstream_module http://nginx.org/en/docs/http/ngx_http_upstream_module.html#sticky S ...
- 【转】dd命令详解及利用dd测试磁盘性能
dd命令详解及利用dd测试磁盘性能 linux下dd命令详解 名称: dd 使用权限: 所有使用者 manpage 定义: convert and copy a file 使用方式: dd [op ...