zoukankan      html  css  js  c++  java
  • HttpServletRequest中得到各种信息

    1、获得domain:

    StringBuffer url = request.getRequestURL();
    String domain = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
    log.info("domain: " + domain);  // http://www.baidu.com/

    例:http://www.baidu.com/index.html

    2、方式二:

    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; 
  • 相关阅读:
    Redis持久化
    Redis配置文件详解
    Linux
    有图有真相
    Redis五大数据类型
    Redis基本知识
    Mysql主从复制
    Python脚本实现KVM虚机添加磁盘
    JQuery制作环形进度条
    JQuery制作标签
  • 原文地址:https://www.cnblogs.com/gmq-sh/p/4958739.html
Copyright © 2011-2022 走看看