java web各种路径获取
来源:互联网 发布:淘宝联盟一直不提现 编辑:程序博客网 时间:2024/05/21 23:34
对于java web各种路径的获取一直有点混乱,找个时间用代码实际测试了一下,做一下记录~
代码:
private void printPath(HttpServletRequest request){ printStr("request.getScheme()", request.getScheme()); printStr("request.getServerName()", request.getServerName()); printStr("request.getServerPort()", request.getServerPort()); printStr("request.getContextPath()", request.getContextPath()); printStr("request.getServletPath()", request.getServletPath()); printStr("request.getRequestURI()", request.getRequestURI()); printStr("request.getPathInfo()", request.getPathInfo()); printStr("request.getLocalPort()", request.getLocalPort()); printStr("request.getLocalAddr()", request.getLocalAddr()); printStr("request.getLocalName()", request.getLocalName()); System.out.println("=========session========"); ServletContext servletContext = request.getSession().getServletContext(); printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("/")); printStr("servletContext.getContextPath()", servletContext.getContextPath()); printStr("servletContext.getServerInfo()", servletContext.getServerInfo()); } private void printStr(String name, Object value){ System.out.println(name + ":" + value); }
输出:
request.getScheme():http request.getServerName():localhost request.getServerPort():8080 request.getContextPath():/myweb request.getServletPath():/test request.getRequestURI():/myweb/test request.getPathInfo():null request.getLocalPort():8080 request.getLocalAddr():0:0:0:0:0:0:0:1 request.getLocalName():0:0:0:0:0:0:0:1 =========session======== servletContext.getRealPath('\'):D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\ servletContext.getContextPath():/myweb servletContext.getServerInfo():Apache Tomcat/7.0.57
-------------------------------------------------万恶的分割线-------------------------------------------------
此处注意一下这个方法:获取绝对路径
servletContext.getRealPath("/")
在web后台,如果想创建一个file对象,需使用到绝对路径。使用时的传值与返回值还是以示例说明:
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("\\"));输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("/"))输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("key.txt"));输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\key.txt//一下几种传值输出一样的printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("//"));printStr("servletContext.getRealPath('\\')", servletContext.getRealPath(""));输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb
在来看一下getRealPath的源码:
@Overridepublic String getRealPath(String path) {Resource resource = this.resourceLoader.getResource(getResourceLocation(path));try {return resource.getFile().getAbsolutePath();}catch (IOException ex) {logger.warn("Couldn't determine real path of resource " + resource, ex);return null;}}getResourceLocation(path)的源码:
/** * Build a full resource location for the given path, prepending the resource * base path of this {@code MockServletContext}. * @param path the path as specified * @return the full resource path */protected String getResourceLocation(String path) {if (!path.startsWith("/")) {path = "/" + path;}return this.resourceBasePath + path;}
到此就明白了,要是获取web根目录的绝对路径,传个空字符串(“”)就ok了,要是根目录下的其他路径,直接跟路径就好咯~
0 0
- java web 各种获取路径
- java web 获取各种路径
- java web各种路径获取
- Java web request获取各种路径总结
- Java获取各种路径
- java获取各种路径 .
- Java获取各种路径
- java 获取各种路径
- java获取各种路径
- JAVA WEB项目中各种路径的获取
- JAVA WEB项目中各种路径的获取
- Java web项目的各种路径的获取
- java web 获取路径
- java获取各种项目路径
- java 中获取各种路径
- web项目中各种路径的获取
- web项目中各种路径的获取
- web项目中各种路径的获取
- 基数排序
- S4.6_Struts2_Annotation action动作类使用annotation注解的配置方式
- webview控件介绍
- android启动页广告欢迎界面
- 一汽丰田RAV4电路图2012至2013
- java web各种路径获取
- 3、webService CXF的应用1 tomcat的配置方法
- MAKEFILE基本写法.
- 5分钟完全理解android handler
- CNN for image retrieval
- TCP的窗口滑动机制与拥塞控制
- SVN提交报错:Unable to connect to a repository at URL
- C语言 简单链式哈希
- 第六章 使用C#开发数据库应用系统 上机练习1-4