zoukankan      html  css  js  c++  java
  • javaweb基础 01--JSP取得绝对路径应用

    1.相关函数说明

    * request.getScheme() 等到的是协议名称,默认是http
    
    * request.getServerName() 得到的是在服务器的配置文件中配置的服务器名称 比如:localhost .baidu.com 等等
    
    * request.getServerPort() 得到的是服务器的配置文件中配置的端口号 比如 8080等等 
    
    * request.getContextPath()  返回站点的根目录
    
    * request.getRealpath("/")得到的是实际的物理路径,也就是你的项目所在服务器中的路径

    2.用法示例

    <%
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort();
        String path = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + request.getContextPath()
                + "/";
        String filePath=path+"resources/";
        session.setAttribute("path", path);
        session.setAttribute("basePath", basePath);
        session.setAttribute("filePath", filePath);
    %>
    
    以上这段代码的project name是drp5.1,可在tomcat下的webapp目录下找到该目录。
    其中 request.getContextPath() = /drp5.1
    basePath = http://localhost:8080
    path = http://localhost:8080/drp5.1/
    filePath = http://localhost:8080/drp5.1/resources/ 
  • 相关阅读:
    转载-如何高效的学习技术
    Lc176-第二高的薪水
    Lc4-寻找两个有序数组的中位数
    Lc175-组合两个表
    Lc3-无重复字符的最长子串
    Lc2-俩数相加
    Lc1- 两数之和
    jpa-子查詢
    20191225页面样式
    leetcode二刷结束
  • 原文地址:https://www.cnblogs.com/polestar/p/7193486.html
Copyright © 2011-2022 走看看