zoukankan      html  css  js  c++  java
  • jsp Request获取url信息的各种方法比较

     1     从Request对象中可以获取各种路径信息,以下例子:  
     2     假设请求的页面是index.jsp,项目是WebDemo,则在index.jsp中获取有关request对象的各种路径信息如下  
     3     String path = request.getContextPath();  
     4     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
     5     String remoteAddress=request.getRemoteAddr();  
     6     String servletPath=request.getServletPath();  
     7     String realPath=request.getRealPath("/");  
     8     String remoteUser=request.getRemoteUser();  
     9     String requestURI=request.getRequestURI();  
    10     out.println("path:"+path+"<br>");  
    11     out.println("basePath:"+basePath+"<br>");  
    12     out.println("remoteAddr:"+remoteAddress+"<br>");  
    13     out.println("servletPath:"+servletPath+"<br>");  
    14     out.println("realPath:"+realPath+"<br>");  
    15     out.println("remoteUser:"+remoteUser+"<br>");  
    16     out.println("requestURI:"+requestURI+"<br>");  
    17     结果:  
    18     path:/WebDemo  
    19     basePath:http://localhost:8683/WebDemo/  
    20     remoteAddr:127.0.0.1  
    21     servletPath:/index.jsp  
    22     realPath:D:apache-tomcat-6.0.13webappsWebDemo  
    23     remoteUser:null  
    24     requestURI:/WebDemo/index.jsp  
    25     从上不难看出request各个对应方法所代表的含义  
     1     从request获取各种路径总结:  
     2     request.getRealPath("url");//虚拟目录映射为实际目录  
     3     request.getRealPath("./");//网页所在的目录  
     4     request.getRealPath("../");//网页所在目录的上一层目录  
     5     假定你的web application(web应用)名称为news,你的浏览器中输入请求路径:http://localhost:8080/uploading/load.jsp  
     6     request.getContextPath()  =>  /uploading  
     7     request.getServletPath()  =>  /load.jsp  
     8     request.getRequestURL()  =>  http://localhost:8080/uploading/load.jsp  
     9     request.getRealPath("/")  =>   F:learn.metadata.pluginsorg.eclipse.wst.server.core	mp0wtpwebappsuploading  
    10     现在request.getRealPath("/") 这个方法已经不推荐使用了  
    11     可以使用  
    12     ServletContext.getRealPath(java.lang.String)  instead.  
    13     request.getSession().getServletContext().getRealPath() 得到工程文件的实际物理路径,也就是绝对地址
     //Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request  
             //  eg.     /manage/editExam.domethod=goExamSet&type=U  
          String url = request.getRequestURI();    
          //The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters  
            //eg.      http://127.0.0.1:8080/manage/editExam.domethod=goExamSet&type=U  
          StringBuffer url_buffer = request.getRequestURL();  
      HttpServletRequest 的这两种方法都只能得到不包含参数的请求url,区别如下:  
    1 前者返回相对路径,后者返回完整路径  
    2 前者返回string ,后者返回stringbuffer  
    得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了  
      String queryString = request.getQueryString();  
    ring fullPath = url + queryString;   // 或者是url_buffer.toString()+queryString; 
  • 相关阅读:
    Excel中输入身份证后3位变成0,怎么办?
    Css中如何使英文和拼音变成全大写、全小写和首字母大写?
    css中 font常用的样式属性
    Css的向左浮动、先右浮动、绝对定位、相对定位的简单使用
    如何解决Css属性text-overflow:ellipsis 不起作用(文本溢出显示省略号)
    mysql 基本语法学习1(数据库、数据表、数据列的操作)
    sql server中如何将两个字段数据合并成一个字段显示(字段与字段添加特殊符号)
    Linq to Entity 求最大小值Max/Min返回null的处理方法
    C#匿名对象在其它方法体内怎么取到相应的值(不想建立对应的类并转化的情况下)?
    【转发】JQuery中操作Css样式的方法
  • 原文地址:https://www.cnblogs.com/linvan/p/6132808.html
Copyright © 2011-2022 走看看