• 转:getContextPath、getServletPath、getRequestURI的区别


    假定你的web application 名称为news,你在浏览器中输入请求路径: 
    http://localhost:8080/news/main/list.jsp 
    则执行下面向行代码后打印出如下结果: 
    1、 System.out.println(request.getContextPath()); //可返回站点的根路径。也就是项目的名字 
    打印结果:/news 
       2、System.out.println(request.getServletPath()); 
    打印结果:/main/list.jsp 
    3、 System.out.println(request.getRequestURI()); 
    打印结果:/news/main/list.jsp 
    4、 System.out.println(request.getRealPath("/")); 
    打印结果:F:Tomcat 6.0webapps ews est 

    -------------------------------------------------------------------

    1、request.getRequestURL()

    返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。
    2、request.getRequestURI()

    得到的是request URL的部分值,并且web容器没有decode过的

    3、request.getContextPath() 
    返回 the context of the request.

    4、request.getServletPath() 
    返回调用servlet部分的url.

    5、request.getQueryString() 
    返回url路径?后面的查询字符串

    示例:

    当前url:http://localhost:8080/CarsiLogCenter_new/idpstat.jsp?action=idp.sptopn

    request.getRequestURL() http://localhost:8080/CarsiLogCenter_new/idpstat.jsp
    request.getRequestURI()  /CarsiLogCenter_new/idpstat.jsp
    request.getContextPath() /CarsiLogCenter_new
    request.getServletPath() /idpstat.jsp

    request.getQueryString() action=idp.sptopn

    1、request.getScheme() 返回当前链接使用的协议;比如,一般应用返回http;SSL返回https;

    2、在程序中的应用如下:

    Java代码  收藏代码
    1. String path = request.getContextPath();  
    2.   
    3. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    4.   
    5. <base href="<%=basePath%>">  

    3、复习一下request 的常用方法:

    request.getSchema()可以返回当前页面使用的协议,http 或是 https;

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

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

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

  • 相关阅读:
    报表-普通表格中的行号
    Wyn BI-条件格式化-以分组为单位设置交替背景色
    报表-表格-背景颜色或背景图片设置
    报表-交叉分析表中的行号
    矩表中如何根据条件隐藏行、列
    仪表板中关于指标值的联动分析设置
    报表设计技巧-使用表格实现多行自由布局报表
    报表设计技巧-矩表向导让报表设计速度提升10倍以上
    容器内外的可视化元素如何设置联动关系
    报表表格数据排序显示
  • 原文地址:https://www.cnblogs.com/zhangshitong/p/5197600.html
Copyright © 2020-2023  润新知