zoukankan      html  css  js  c++  java
  • javaweb获取项目路径的方法

    在jsp和class文件中调用的相对路径不同。 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProperty("user.dir")获取你工程的绝对路径。


    另:在Jsp,Servlet,Java中详细获得路径的方法!

    1.jsp中取得路径:

    以工程名为TEST为例:


    (1)得到包含工程名的当前页面全路径:request.getRequestURI()
    结果:/TEST/test.jsp
    (2)得到工程名:request.getContextPath()
    结果:/TEST
    (3)得到当前页面所在目录下全名称:request.getServletPath()
    结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
    (4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
    结果:D: esinwebappsTEST est.jsp
    (5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
    结果:D: esinwebappsTEST


    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:TomcatwebappsTEST
    (2)得到IE地址栏地址:request.getRequestURL()
    结果:http://localhost:8080/TEST/test
    (3)得到相对地址:request.getRequestURI()
    结果:/TEST/test

    <%
    String path = request.getContextPath();//
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    //basePath=http://localhost:8080/TEST
    %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    pageContext.setAttribute("basePath",basePath);
    %>
    <c:set var="proPath" value="${pageContext.request.contextPath}" /> 获取当前项目名称
    <c:set var="basePath" value="${basePath}" />获取当前项目的全路径到项目的名称即http://localhost:8080/TEST
    ---------------------
    作者:tEnshiYang
    来源:CSDN
    原文:https://blog.csdn.net/qq_34445857/article/details/79223526
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    Python-OpenCV学习(三):图像的表示
    Python-OpenCV学习(一):OpenCV结构
    Numpy安装+导入出错的解决方法
    可信链测评
    《机器学习》第二周 多元线性回归
    《机器学习》第一周 一元回归法 | 模型和代价函数,梯度下降
    Ubutu16.04安装Transmisson
    Ubuntu “载入软件包列表失败” |“ 下载软件仓库信息失败”的解决方法
    《机器学习》第一周 Introduction | 监督学习与非监督学习的基本概念
    有道词典在Ubutu16.04上的安装
  • 原文地址:https://www.cnblogs.com/zp-uestc/p/10320250.html
Copyright © 2011-2022 走看看