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() 得到工程文件的实际物理路径,也就是绝对地址  



      1.       //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  
      2.          //  eg.     /manage/editExam.domethod=goExamSet&type=U  
      3.       String url = request.getRequestURI();    
      4.       //The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters  
      5.         //eg.      http://127.0.0.1:8080/manage/editExam.domethod=goExamSet&type=U  
      6.       StringBuffer url_buffer = request.getRequestURL();  
      7.   HttpServletRequest 的这两种方法都只能得到不包含参数的请求url,区别如下:  
      8. 1 前者返回相对路径,后者返回完整路径  
      9. 2 前者返回string ,后者返回stringbuffer  
      10. 得到完整请求url可以通过如下方法,getQueryString()得到的是url后面的参数串,和前者相加就是带参数的请求路径了  
      11.   String queryString = request.getQueryString();  
      12. ring fullPath = url + queryString;   // 或者是url_buffer.toString()+queryString; 
  • 相关阅读:
    LeetCode 382. Linked List Random Node
    LeetCode 398. Random Pick Index
    LeetCode 1002. Find Common Characters
    LeetCode 498. Diagonal Traverse
    LeetCode 825. Friends Of Appropriate Ages
    LeetCode 824. Goat Latin
    LeetCode 896. Monotonic Array
    LeetCode 987. Vertical Order Traversal of a Binary Tree
    LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
    LeetCode 636. Exclusive Time of Functions
  • 原文地址:https://www.cnblogs.com/1234abcd/p/5525374.html
Copyright © 2011-2022 走看看