zoukankan      html  css  js  c++  java
  • 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-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt
    realpath index.jsp:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxtindex.jsp
    realpath ./:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt.
    realpath ../:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt..
    contextpath:/jyxt
    web项目的全路径:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt
    URI:/jyxt/hello
    URL:http://localhost:8080/jyxt/hello
    servletpath:/hello
    ~~~~~~~~~~~~~~~页面~~~~~~~~~~~~~~~~~
    realpath null:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt
    realpath index.jsp:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxtindex.jsp
    realpath ./:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt.
    realpath ../:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt..
    contextpath:/jyxt
    web项目的全路径:D:eclipse-jee-luna-SR1-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxt
    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-win32eclipseworkspace.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsjyxtjyxtWEB-INFview
    

    在类中获取路径先不看,其他的只有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()
  • 相关阅读:
    MVVM CopyValuesTo接触属性上下级关联
    怎样控制WPF GroupBox.HeaderTemplate中的控件
    wpf动态创建DataGrid
    mvvm 绑定textbox焦点丢失问题
    C# 导出CSV文件
    使用C#选择文件夹、打开文件夹、选择文件或者如何使用C#选择文件夹
    EF 强制从数据库刷新集合
    WPF及Silverlight中将DataGrid数据导出
    VS 2005中winForm开发(C#)—图片上传到数据库与显示(sql server 2005)
    数据导出为csv文件时 数值型数据为科学计数法 时间被截取的解决方法
  • 原文地址:https://www.cnblogs.com/wang-jing/p/4620157.html
Copyright © 2011-2022 走看看