• js或jsp 获取项目路径常用方法


    可以引入一个jsp,这个jsp的内容如下:

    <%@page pageEncoding="UTF-8" contentType="text/javascript; charset=UTF-8"%>
    //设置ContextPath
    var __ctx='<%=request.getContextPath()%>';

    <c:set var="ctx" value="${pageContext.request.contextPath}" />

    以后的jsp中,可以这样用

    <script type="text/javascript" src="${ctx}/js/dynamic.jsp"></script>

    或者

    window.location.href =__ctx+ "/zjdlbb/zjdlbball/zjdlbball/list.ht";

    知识扩展:

    getContextPath()是jsp获取路径的一种方式,返回当前页面所在的应用的名字

    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    <base href="<%=basePath%>">

           request.getSchema(),返回的是当前 连接使用的协议,一般应用返回的是http、SSL返回的是https;

           request.getServerName(),返回当前页面所在的 服务器的名字

           request.getServerPort(),返回当前页面所在的服务器使用的端口,80;

           request.getContextPath(),返回当前页面所在的应用的名字

           getContextPath()

           以访问的jsp为:http://localhost:8080/oms/rep/index.jsp,工程名为/dmsb为例:

            request.getContextPath(),得到工程名:/oms;

            request.getServletPath(),返回当前页面所在目录下全名称:/rep/index.jsp;

            request.getRequestURL(),返回IE地址栏地址:http://localhost:8080/oms/rep/index.jsp;

            request.getRequestURI() ,返回包含工程名的当前页面全路径:/oms/rep/index.jsp。

  • 相关阅读:
    TypeError: 'ExcelData' object is not iterable
    输出重定向
    联想Thinkpad T450 屏幕更换记录
    C-sysytem命令的使用
    CMD命令大全
    Ubuntu16.04 添加 Docker用户组
    Docker学习
    Linux系统卡死后紧急处理
    Django之ModelForm详解
    Django的ORM实现数据库事务操作
  • 原文地址:https://www.cnblogs.com/rdchen/p/9705466.html
Copyright © 2020-2023  润新知