zoukankan      html  css  js  c++  java
  • Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

    1 区别

    假定你的web application 名称为news,你在浏览器中输入请求路径:

    http://localhost:8080/news/main/list.jsp

    1.1 System.out.println(request.getContextPath());

    打印结果:/news

    1.2 System.out.println(request.getServletPath());

    打印结果:/main/list.jsp

    1.3 System.out.println(request.getRequestURI());

    打印结果:/news/main/list.jsp

    1.4 System.out.println(request.getRealPath("/"));

    打印结果:F:Tomcat 6.0webapps ews est


    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path

    这其实就是 获得应用的根url,比如说你的应用的根路径是 http://localhost:8080,那么你列出的代码就是为basePath赋值为 http://localhost:8080
    具体点:
    1、request.getScheme() 返回协议的名称 http,和后面的"://" 拼起来就成了 http://
    2、request.getServerName() 这是获取你的服务器的名称,如果你的应用部署在本机那么其就返回localhost或者127.0.0.1 ,这2个是等价的
    3、request.getServerPort() 是你应用使用的端口,比如8080或者80 等等
  • 相关阅读:
    Java API学习
    Java接口类学习笔记
    TCP/IP协议族
    Web服务器
    Redis笔记
    Python知识总汇
    GIL锁
    day8
    day7
    day6
  • 原文地址:https://www.cnblogs.com/qingfengzhuimeng/p/5092936.html
Copyright © 2011-2022 走看看