zoukankan      html  css  js  c++  java
  • web项目的getContextPath()

               伯乐一看小编的这个博文的标题是不是觉得有些小,以点到面,知道了web中getContextPath()这种获取路径的方式,显然其他的方式的是可以以此类推的。常说,工作学习找共同点嘛。

           上一段我们也提高getContextPath()的含义,是jsp中获取路径的一种方式,返回当前页面所在的应用的名字。知其然知其所以然,知道它大体上的含义,看看在项目中是如何使用的?

    1. String path = request.getContextPath();  
    2. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    3. <base href="<%=basePath%>">  
    在程序中我们一般这样使用。说到了这里,我们可以看看request常用的方法:

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

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

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

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

           getContextPath()是jsp中获取路径的一种方式,那么获取到结果是什么呢?jsp其他获取路径的方式是怎样的呢?表示好奇,请听分解:

           以访问的jsp为:http://localhost:8080/dmsd-itoo-exam-log-web/course/index.jsp,工程名为/dmsd-itoo-exam-log-web为例:

            request.getContextPath(),得到工程名:/dmsd-itoo-exam-log-web

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

            request.getRequestURL(),返回IE地址栏地址:http://localhost:8080/dmsd-itoo-exam-log-web/course/index.jsp;

            request.getRequestURI() ,返回包含工程名的当前页面全路径:/dmsd-itoo-exam-log-web/course/index.jsp。
           对jsp获取当前应用的路径方法和request常用方法有了基本的了解,下面后台通过request.getScheme()获取当前页面使用协议遇到的问题:request.getScheme()获取到的确实http而不是https,与request.getRequestURL()拿到的一样也是http。

           这是为什么呢?

           原来,nginx+tomcat部署web服务,tomcat接收到的请求是来自nginx的http请求。我想这也是上一博文中说到项目用https启动请求不到页面的原因吧。

  • 相关阅读:
    spring security 获取当前用户
    spring data jpa deleteInBatch 导致异常 java.lang.StackOverflowError
    大数据 分布式文件系统 HDFS概念
    angular9 ng start正常,build部署后无法正常加载页面
    springboot使用rocketmq RocketMQMessageListener参数
    spring boot使用rocketmq
    Teamcenter RAC 调用查询
    Teamcenter RAC 查找数据集并获取数据集中文件
    带有编译时和运行时的简记
    前台线程和后台线程、线程安全
  • 原文地址:https://www.cnblogs.com/jpfss/p/9121613.html
Copyright © 2011-2022 走看看